Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Discovery / YamlDirectoryDiscoveryTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Discovery;
4
5 use Drupal\Component\Discovery\DiscoveryException;
6 use Drupal\Component\Discovery\YamlDirectoryDiscovery;
7 use Drupal\Tests\UnitTestCase;
8 use org\bovigo\vfs\vfsStream;
9
10 /**
11  * YamlDirectoryDiscoveryTest component unit tests.
12  *
13  * @coversDefaultClass \Drupal\Component\Discovery\YamlDirectoryDiscovery
14  *
15  * @group Discovery
16  */
17 class YamlDirectoryDiscoveryTest extends UnitTestCase {
18
19   /**
20    * Tests YAML directory discovery.
21    *
22    * @covers ::findAll
23    */
24   public function testDiscovery() {
25     vfsStream::setup('modules', NULL, [
26       'test_1' => [
27         'subdir1' => [
28           'item_1.test.yml' => "id: item1\nname: 'test1 item 1'",
29         ],
30         'subdir2' => [
31           'item_2.test.yml' => "id: item2\nname: 'test1 item 2'",
32         ],
33       ],
34       'test_2' => [
35         'subdir1' => [
36           'item_3.test.yml' => "id: item3\nname: 'test2 item 3'",
37         ],
38         'subdir2' => [],
39       ],
40       'test_3' => [],
41       'test_4' => [
42         'subdir1' => [
43           'item_4.test.yml' => "id: item4\nname: 'test4 item 4'",
44           'item_5.test.yml' => "id: item5\nname: 'test4 item 5'",
45           'item_6.test.yml' => "id: item6\nname: 'test4 item 6'",
46         ],
47       ],
48     ]);
49
50     // Set up the directories to search.
51     $directories = [
52       // Multiple directories both with valid items.
53       'test_1' => [
54         vfsStream::url('modules/test_1/subdir1'),
55         vfsStream::url('modules/test_1/subdir2'),
56       ],
57       // The subdir2 directory is empty.
58       'test_2' => [
59         vfsStream::url('modules/test_2/subdir1'),
60         vfsStream::url('modules/test_2/subdir2'),
61       ],
62       // Directories that do not exist.
63       'test_3' => [
64         vfsStream::url('modules/test_3/subdir1'),
65         vfsStream::url('modules/test_3/subdir2'),
66       ],
67       // A single directory.
68       'test_4' => vfsStream::url('modules/test_4/subdir1'),
69     ];
70
71     $discovery = new YamlDirectoryDiscovery($directories, 'test');
72     $data = $discovery->findAll();
73
74     $this->assertSame(['id' => 'item1', 'name' => 'test1 item 1', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/subdir1/item_1.test.yml'], $data['test_1']['item1']);
75     $this->assertSame(['id' => 'item2', 'name' => 'test1 item 2', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/subdir2/item_2.test.yml'], $data['test_1']['item2']);
76     $this->assertCount(2, $data['test_1']);
77
78     $this->assertSame(['id' => 'item3', 'name' => 'test2 item 3', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_2/subdir1/item_3.test.yml'], $data['test_2']['item3']);
79     $this->assertCount(1, $data['test_2']);
80
81     $this->assertTrue(empty($data['test_3']), 'test_3 provides 0 items');
82
83     $this->assertSame(['id' => 'item4', 'name' => 'test4 item 4', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_4.test.yml'], $data['test_4']['item4']);
84     $this->assertSame(['id' => 'item5', 'name' => 'test4 item 5', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_5.test.yml'], $data['test_4']['item5']);
85     $this->assertSame(['id' => 'item6', 'name' => 'test4 item 6', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_4/subdir1/item_6.test.yml'], $data['test_4']['item6']);
86     $this->assertCount(3, $data['test_4']);
87   }
88
89   /**
90    * Tests YAML directory discovery with an alternate ID key.
91    *
92    * @covers ::findAll
93    */
94   public function testDiscoveryAlternateId() {
95     vfsStream::setup('modules', NULL, [
96       'test_1' => [
97         'item_1.test.yml' => "alt_id: item1\nid: ignored",
98       ],
99     ]);
100
101     // Set up the directories to search.
102     $directories = ['test_1' => vfsStream::url('modules/test_1')];
103
104     $discovery = new YamlDirectoryDiscovery($directories, 'test', 'alt_id');
105     $data = $discovery->findAll();
106
107     $this->assertSame(['alt_id' => 'item1', 'id' => 'ignored', YamlDirectoryDiscovery::FILE_KEY => 'vfs://modules/test_1/item_1.test.yml'], $data['test_1']['item1']);
108     $this->assertCount(1, $data['test_1']);
109   }
110
111   /**
112    * Tests YAML directory discovery with a missing ID key.
113    *
114    * @covers ::findAll
115    * @covers ::getIdentifier
116    */
117   public function testDiscoveryNoIdException() {
118     $this->setExpectedException(DiscoveryException::class, 'The vfs://modules/test_1/item_1.test.yml contains no data in the identifier key \'id\'');
119     vfsStream::setup('modules', NULL, [
120       'test_1' => [
121         'item_1.test.yml' => "",
122       ],
123     ]);
124
125     // Set up the directories to search.
126     $directories = ['test_1' => vfsStream::url('modules/test_1')];
127
128     $discovery = new YamlDirectoryDiscovery($directories, 'test');
129     $discovery->findAll();
130   }
131
132   /**
133    * Tests YAML directory discovery with invalid YAML.
134    *
135    * @covers ::findAll
136    */
137   public function testDiscoveryInvalidYamlException() {
138     $this->setExpectedException(DiscoveryException::class, 'The vfs://modules/test_1/item_1.test.yml contains invalid YAML');
139     vfsStream::setup('modules', NULL, [
140       'test_1' => [
141         'item_1.test.yml' => "id: invalid\nfoo : [bar}",
142       ],
143     ]);
144
145     // Set up the directories to search.
146     $directories = ['test_1' => vfsStream::url('modules/test_1')];
147
148     $discovery = new YamlDirectoryDiscovery($directories, 'test');
149     $discovery->findAll();
150   }
151
152 }