Completed
Pull Request — 8.x-3.x (#501)
by Philipp
04:28
created

MenuTest::testMenuInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
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_core\Kernel\GraphQLCoreTestBase;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Test access to menu items.
12
 *
13
 * @group graphql_menu
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() {
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->assertTrue($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
    // TODO: Check cache metadata.
76
    $metadata = $this->defaultCacheMetaData();
77
    $metadata->addCacheTags([
78
      'config:system.menu.test',
79
    ]);
80
81
    $this->assertResults(
82
      $this->getQueryFromFile('menu.gql'),
83
      [],
84
      [
85
        'info' => [
86
          'name' => 'Test menu',
87
          'description' => 'Menu for testing GraphQL menu access.',
88
        ],
89
        'menu' => [
90
          'links' => [
91
            0 => [
92
              'label' => 'Accessible',
93
              'route' => [
94
                'path' => '/graphql/test/accessible',
95
                'routed' => TRUE,
96
              ],
97
              'attribute' => NULL,
98
              'links' => [
99
                0 => [
100
                  'label' => 'Nested A',
101
                  'attribute' => NULL,
102
                  'route' => [
103
                    'path' => '/graphql/test/accessible',
104
                    'routed' => TRUE,
105
                  ],
106
                ],
107
                1 => [
108
                  'label' => 'Nested B',
109
                  'route' => [
110
                    'path' => '/graphql/test/accessible',
111
                    'routed' => TRUE,
112
                  ],
113
                  'attribute' => NULL,
114
                ],
115
              ],
116
            ],
117
            1 => [
118
              'label' => 'Inaccessible',
119
              'route' => [
120
                'path' => '/',
121
                'routed' => TRUE,
122
              ],
123
              'attribute' => NULL,
124
              'links' => [],
125
            ],
126
            2 => [
127
              'label' => 'Drupal',
128
              'route' => [
129
                'path' => 'http://www.drupal.org',
130
                'routed' => FALSE,
131
              ],
132
              'attribute' => NULL,
133
              'links' => [],
134
            ],
135
          ],
136
        ],
137
      ],
138
      $metadata
139
    );
140
  }
141
}
142