c4718235102d748f4e267d5a0fc820765da703f8
[yaffs-website] / web / core / modules / menu_ui / tests / src / Traits / MenuUiTrait.php
1 <?php
2
3 namespace Drupal\Tests\menu_ui\Traits;
4
5 /**
6  * Provides common methods for Menu UI module tests.
7  */
8 trait MenuUiTrait {
9
10   /**
11    * Asserts that a menu fetched from the database matches an expected one.
12    *
13    * @param array $expected_item
14    *   Array containing properties to check.
15    * @param int $menu_plugin_id
16    *   Menu item id.
17    */
18   protected function assertMenuLink(array $expected_item, $menu_plugin_id) {
19     // Retrieve the menu link.
20     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
21     $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
22     $menu_link_manager->resetDefinitions();
23     // Reset the static load cache.
24     \Drupal::entityTypeManager()->getStorage('menu_link_content')->resetCache();
25     $definition = $menu_link_manager->getDefinition($menu_plugin_id);
26
27     $entity = NULL;
28
29     // Pull the path from the menu link content.
30     if (strpos($menu_plugin_id, 'menu_link_content') === 0) {
31       list(, $uuid) = explode(':', $menu_plugin_id, 2);
32       /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
33       $entity = \Drupal::service('entity.repository')
34         ->loadEntityByUuid('menu_link_content', $uuid);
35     }
36
37     if (isset($expected_item['children'])) {
38       $child_ids = array_values($menu_link_manager->getChildIds($menu_plugin_id));
39       sort($expected_item['children']);
40       if ($child_ids) {
41         sort($child_ids);
42       }
43       $this->assertSame($expected_item['children'], $child_ids);
44       unset($expected_item['children']);
45     }
46
47     if (isset($expected_item['parents'])) {
48       $parent_ids = array_values($menu_link_manager->getParentIds($menu_plugin_id));
49       $this->assertSame($expected_item['parents'], $parent_ids);
50       unset($expected_item['parents']);
51     }
52
53     if (isset($expected_item['langcode']) && $entity) {
54       $this->assertEquals($expected_item['langcode'], $entity->langcode->value);
55       unset($expected_item['langcode']);
56     }
57
58     if (isset($expected_item['enabled']) && $entity) {
59       $this->assertEquals($expected_item['enabled'], $entity->enabled->value);
60       unset($expected_item['enabled']);
61     }
62
63     foreach ($expected_item as $key => $value) {
64       $this->assertNotNull($definition[$key]);
65       $this->assertSame($value, $definition[$key]);
66     }
67   }
68
69 }