Completed
Push — 8.x-3.x ( 32ff4d...d01c55 )
by Sebastian
05:23 queued 02:25
created

MenuTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Menu;
4
5
use Drupal\Core\Menu\MenuTreeParameters;
6
use Drupal\menu_link_content\Entity\MenuLinkContent;
7
use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Test access to menu items.
12
 *
13
 * @group graphql_menu
14
 */
15
class MenuTest extends GraphQLFileTestBase {
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static $modules = [
21
    'system',
22
    'menu_link_content',
23
    'link',
24
    'graphql_core',
25
    'graphql_test_menu',
26
  ];
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  protected function setUp() {
32
    parent::setUp();
33
    $this->installEntitySchema('menu_link_content');
34
    $this->installConfig('menu_link_content');
35
    $this->installConfig('graphql_test_menu');
36
37
    $externalLink = MenuLinkContent::create([
38
      'title' => 'Drupal',
39
      'link' => ['uri' => 'http://www.drupal.org'],
40
      'menu_name' => 'test',
41
      'external' => 1,
42
      'enabled' => 1,
43
      'weight' => 5,
44
    ]);
45
46
    $externalLink->save();
47
48
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager */
49
    $menuLinkManager = $this->container->get('plugin.manager.menu.link');
50
    $menuLinkManager->rebuild();
51
  }
52
53
  /**
54
   * Test if the test setup itself is successful.
55
   */
56
  public function testTestSetup() {
57
    /** @var \Drupal\Core\Menu\MenuTreeStorageInterface $menuStorage */
58
    $menuStorage = $this->container->get('entity_type.manager')->getStorage('menu');
59
    $menu = $menuStorage->load('test');
60
    $this->assertTrue($menu);
61
62
    /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menuTree */
63
    $menuTree = $this->container->get('menu.link_tree');
64
    $this->assertEquals(count($menuTree->load('test', new MenuTreeParameters())), 3);
65
66
    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel */
67
    $httpKernel = $this->container->get('http_kernel');
68
69
    $this->assertEquals($httpKernel->handle(Request::create('/graphql/test/accessible'))->getStatusCode(), 200);
70
    $this->assertEquals($httpKernel->handle(Request::create('/graphql/test/inaccessible'))->getStatusCode(), 403);
71
  }
72
73
  /**
74
   * Test if menu information is returned by GraphQL.
75
   */
76
  public function testMenuInfo() {
77
    $result = $this->executeQueryFile('menu.gql');
78
79
    $this->assertArrayHasKey('data', $result);
80
81
    $this->assertArraySubset([
82
      'info' => [
83
        'name' => 'Test menu',
84
        'description' => 'Menu for testing GraphQL menu access.',
85
      ],
86
    ], $result['data'], "Menu contains correct title and description.");
87
  }
88
89
  /**
90
   * Test menu tree data retrieval.
91
   */
92
  public function testMenuTree() {
93
94
    $result = $this->executeQueryFile('menu.gql');
95
96
    $this->assertArrayHasKey('data', $result);
97
98
    $this->assertArraySubset([
99
      'menu' => [
100
        'links' => [
101
          0 => [
102
            'label' => 'Accessible',
103
            'route' => [
104
              'path' => '/graphql/test/accessible',
105
              'routed' => TRUE,
106
            ],
107
          ],
108
        ],
109
      ],
110
    ], $result['data'], 'Accessible root item is returned.');
111
112
    $this->assertArraySubset([
113
      'menu' => [
114
        'links' => [
115
          0 => [
116
            'links' => [
117
              0 => [
118
                'label' => 'Nested A',
119
                'route' => [
120
                  'path' => '/graphql/test/accessible',
121
                  'routed' => TRUE,
122
                ],
123
              ],
124
            ],
125
          ],
126
        ],
127
      ],
128
    ], $result['data'], 'Accessible nested item A is returned.');
129
130
    $this->assertArraySubset([
131
      'menu' => [
132
        'links' => [
133
          0 => [
134
            'links' => [
135
              1 => [
136
                'label' => 'Nested B',
137
                'route' => [
138
                  'path' => '/graphql/test/accessible',
139
                  'routed' => TRUE,
140
                ],
141
              ],
142
            ],
143
          ],
144
        ],
145
      ],
146
    ], $result['data'], 'Accessible nested item B is returned.');
147
148
    $this->assertArraySubset([
149
      'menu' => [
150
        'links' => [
151
          1 => [
152
            'label' => 'Inaccessible',
153
            'route' => [
154
              'path' => '/',
155
              'routed' => TRUE,
156
            ],
157
          ],
158
        ],
159
      ],
160
    ], $result['data'], 'Inaccessible root item is obfuscated.');
161
162
    $inaccessibleChildren = $result['data']['menu']['links'][1]['links'];
163
    $this->assertEmpty($inaccessibleChildren, 'Inaccessible items do not expose children.');
164
165
    $this->assertArraySubset([
166
      'menu' => [
167
        'links' => [
168
          2 => [
169
            'label' => 'Drupal',
170
            'route' => [
171
              'path' => 'http://www.drupal.org',
172
              'routed' => FALSE,
173
            ],
174
          ],
175
        ],
176
      ],
177
    ], $result['data'], 'External menu link is included properly.');
178
  }
179
180
}
181