MenuTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 63
c 1
b 0
f 0
dl 0
loc 112
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testMenuTree() 0 53 1
A testTestSetup() 0 15 1
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Menu;
4
5
use Drupal\Core\Menu\MenuTreeParameters;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuTreeParameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\menu_link_content\Entity\MenuLinkContent;
0 ignored issues
show
Bug introduced by
The type Drupal\menu_link_content\Entity\MenuLinkContent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Tests\graphql_core\Kernel\GraphQLCoreTestBase;
8
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Test access to menu items.
12
 *
13
 * @group graphql_core
14
 */
15
class MenuTest extends GraphQLCoreTestBase {
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static $modules = [
21
    'menu_link_content',
22
    'link',
23
    'graphql_test_menu',
24
  ];
25
26
  /**
27
   * {@inheritdoc}
28
   */
29
  protected function setUp(): void {
30
    parent::setUp();
31
    $this->installEntitySchema('menu_link_content');
32
    $this->installConfig('menu_link_content');
33
    $this->installConfig('graphql_test_menu');
34
35
    $externalLink = MenuLinkContent::create([
36
      'title' => 'Drupal',
37
      'link' => ['uri' => 'http://www.drupal.org'],
38
      'menu_name' => 'test',
39
      'external' => 1,
40
      'enabled' => 1,
41
      'weight' => 5,
42
    ]);
43
44
    $externalLink->save();
45
46
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager */
47
    $menuLinkManager = $this->container->get('plugin.manager.menu.link');
48
    $menuLinkManager->rebuild();
49
  }
50
51
  /**
52
   * Test if the test setup itself is successful.
53
   */
54
  public function testTestSetup() {
55
    /** @var \Drupal\Core\Menu\MenuTreeStorageInterface $menuStorage */
56
    $menuStorage = $this->container->get('entity_type.manager')->getStorage('menu');
57
    $menu = $menuStorage->load('test');
58
    $this->assertIsObject($menu);
59
60
    /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menuTree */
61
    $menuTree = $this->container->get('menu.link_tree');
62
    $this->assertEquals(count($menuTree->load('test', new MenuTreeParameters())), 3);
63
64
    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel */
65
    $httpKernel = $this->container->get('http_kernel');
66
67
    $this->assertEquals($httpKernel->handle(Request::create('/graphql/test/accessible'))->getStatusCode(), 200);
68
    $this->assertEquals($httpKernel->handle(Request::create('/graphql/test/inaccessible'))->getStatusCode(), 403);
69
  }
70
71
  /**
72
   * Test menu tree data retrieval.
73
   */
74
  public function testMenuTree() {
75
    $metadata = $this->defaultCacheMetaData();
76
    $metadata->addCacheTags(['config:system.menu.test']);
77
78
    $this->assertResults(
79
      $this->getQueryFromFile('menu.gql'),
80
      [],
81
      [
82
        'info' => [
83
          'name' => 'Test menu',
84
          'description' => 'Menu for testing GraphQL menu access.',
85
        ],
86
        'menu' => [
87
          'links' => [
88
            0 => [
89
              'label' => 'Accessible',
90
              'route' => [
91
                'path' => '/graphql/test/accessible',
92
                'routed' => TRUE,
93
              ],
94
              'attribute' => NULL,
95
              'links' => [
96
                0 => [
97
                  'label' => 'Nested A',
98
                  'attribute' => NULL,
99
                  'route' => [
100
                    'path' => '/graphql/test/accessible',
101
                    'routed' => TRUE,
102
                  ],
103
                ],
104
                1 => [
105
                  'label' => 'Nested B',
106
                  'route' => [
107
                    'path' => '/graphql/test/accessible',
108
                    'routed' => TRUE,
109
                  ],
110
                  'attribute' => NULL,
111
                ],
112
              ],
113
            ],
114
            1 => [
115
              'label' => 'Drupal',
116
              'route' => [
117
                'path' => 'http://www.drupal.org',
118
                'routed' => FALSE,
119
              ],
120
              'attribute' => NULL,
121
              'links' => [],
122
            ],
123
          ],
124
        ],
125
      ],
126
      $metadata
127
    );
128
  }
129
130
}
131