|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\Tests\localgov_subsites\Kernel; |
| 6 | + |
| 7 | +use Drupal\field\Entity\FieldConfig; |
| 8 | +use Drupal\field\Entity\FieldStorageConfig; |
| 9 | +use Drupal\KernelTests\KernelTestBase; |
| 10 | +use Drupal\localgov_subsites\Plugin\Block\SubsitesHierarchyTrait; |
| 11 | +use Drupal\node\NodeInterface; |
| 12 | +use Drupal\Tests\node\Traits\ContentTypeCreationTrait; |
| 13 | +use Drupal\Tests\node\Traits\NodeCreationTrait; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test SubsitesHierarchyTrait methods. |
| 17 | + * |
| 18 | + * @group localgov_subsites |
| 19 | + */ |
| 20 | +class SubsitesHierarchyTraitTest extends KernelTestBase { |
| 21 | + |
| 22 | + use ContentTypeCreationTrait; |
| 23 | + use NodeCreationTrait; |
| 24 | + use SubsitesHierarchyTrait; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + protected static $modules = [ |
| 30 | + 'dbal', |
| 31 | + 'field', |
| 32 | + 'filter', |
| 33 | + 'entity_hierarchy', |
| 34 | + 'language', |
| 35 | + 'link', |
| 36 | + 'node', |
| 37 | + 'path', |
| 38 | + 'system', |
| 39 | + 'text', |
| 40 | + 'token', |
| 41 | + 'user', |
| 42 | + ]; |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + protected function setUp(): void { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->installEntitySchema('user'); |
| 51 | + $this->installEntitySchema('node'); |
| 52 | + $this->installSchema('node', ['node_access']); |
| 53 | + $this->installConfig([ |
| 54 | + 'filter', |
| 55 | + 'system', |
| 56 | + 'user', |
| 57 | + 'node', |
| 58 | + ]); |
| 59 | + |
| 60 | + // Create subsite content types and entity hierarchy field. |
| 61 | + $this->createContentType([ |
| 62 | + 'type' => 'localgov_subsites_overview', |
| 63 | + 'name' => 'Subsites Overview', |
| 64 | + ]); |
| 65 | + $this->createContentType([ |
| 66 | + 'type' => 'localgov_subsites_page', |
| 67 | + 'name' => 'Subsites Page', |
| 68 | + ]); |
| 69 | + $storage = FieldStorageConfig::create([ |
| 70 | + 'entity_type' => 'node', |
| 71 | + 'field_name' => 'localgov_subsites_parent', |
| 72 | + 'id' => 'node.localgov_subsites_parent', |
| 73 | + 'type' => 'entity_reference_hierarchy', |
| 74 | + 'settings' => [ |
| 75 | + 'target_type' => 'node', |
| 76 | + ], |
| 77 | + ]); |
| 78 | + $storage->save(); |
| 79 | + $config = FieldConfig::create([ |
| 80 | + 'field_name' => 'localgov_subsites_parent', |
| 81 | + 'entity_type' => 'node', |
| 82 | + 'bundle' => 'localgov_subsites_page', |
| 83 | + 'id' => 'node.localgov_subsites_page.localgov_subsites_parent', |
| 84 | + 'label' => 'Parent', |
| 85 | + ]); |
| 86 | + $config->save(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test callback. |
| 91 | + */ |
| 92 | + public function testGetFlattenedSubsiteHierarchy(): void { |
| 93 | + |
| 94 | + // Create subsite hierarchy. |
| 95 | + $subsite_overview = $this->createNode([ |
| 96 | + 'title' => $this->randomString(), |
| 97 | + 'type' => 'localgov_subsites_overview', |
| 98 | + 'status' => NodeInterface::PUBLISHED, |
| 99 | + ]); |
| 100 | + $subsite_overview->save(); |
| 101 | + $subsite_page1 = $this->createNode([ |
| 102 | + 'title' => $this->randomString(), |
| 103 | + 'type' => 'localgov_subsites_page', |
| 104 | + 'localgov_subsites_parent' => [ |
| 105 | + 'target_id' => $subsite_overview->id(), |
| 106 | + 'weight' => 0, |
| 107 | + ], |
| 108 | + 'status' => NodeInterface::NOT_PUBLISHED, |
| 109 | + ]); |
| 110 | + $subsite_page1->save(); |
| 111 | + $subsite_page2 = $this->createNode([ |
| 112 | + 'title' => $this->randomString(), |
| 113 | + 'type' => 'localgov_subsites_page', |
| 114 | + 'localgov_subsites_parent' => [ |
| 115 | + 'target_id' => $subsite_overview->id(), |
| 116 | + 'weight' => 0, |
| 117 | + ], |
| 118 | + 'status' => NodeInterface::PUBLISHED, |
| 119 | + ]); |
| 120 | + $subsite_page2->save(); |
| 121 | + $subsite_page3 = $this->createNode([ |
| 122 | + 'title' => $this->randomString(), |
| 123 | + 'type' => 'localgov_subsites_page', |
| 124 | + 'localgov_subsites_parent' => [ |
| 125 | + 'target_id' => $subsite_page2->id(), |
| 126 | + 'weight' => 0, |
| 127 | + ], |
| 128 | + 'status' => NodeInterface::PUBLISHED, |
| 129 | + ]); |
| 130 | + $subsite_page3->save(); |
| 131 | + |
| 132 | + $this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_overview)); |
| 133 | + $this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page1)); |
| 134 | + $this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page2)); |
| 135 | + $this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page3)); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments