More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Extension / InfoParserUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Extension;
4
5 use Drupal\Core\Extension\InfoParser;
6 use Drupal\Tests\UnitTestCase;
7 use org\bovigo\vfs\vfsStream;
8
9 /**
10  * Tests InfoParser class and exception.
11  *
12  * Files for this test are stored in core/modules/system/tests/fixtures and end
13  * with .info.txt instead of info.yml in order not not be considered as real
14  * extensions.
15  *
16  * @coversDefaultClass \Drupal\Core\Extension\InfoParser
17  *
18  * @group Extension
19  */
20 class InfoParserUnitTest extends UnitTestCase {
21
22   /**
23    * The InfoParser object.
24    *
25    * @var \Drupal\Core\Extension\InfoParser
26    */
27   protected $infoParser;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->infoParser = new InfoParser();
36   }
37
38   /**
39    * Tests the functionality of the infoParser object.
40    *
41    * @covers ::parse
42    */
43   public function testInfoParserNonExisting() {
44     vfsStream::setup('modules');
45     $info = $this->infoParser->parse(vfsStream::url('modules') . '/does_not_exist.info.txt');
46     $this->assertTrue(empty($info), 'Non existing info.yml returns empty array.');
47   }
48
49   /**
50    * Test if correct exception is thrown for a broken info file.
51    *
52    * @covers ::parse
53    */
54   public function testInfoParserBroken() {
55     $broken_info = <<<BROKEN_INFO
56 # info.yml for testing broken YAML parsing exception handling.
57 name: File
58 type: module
59 description: 'Defines a file field type.'
60 package: Core
61 version: VERSION
62 core: 8.x
63 dependencies::;;
64   - field
65 BROKEN_INFO;
66
67     vfsStream::setup('modules');
68     vfsStream::create([
69       'fixtures' => [
70         'broken.info.txt' => $broken_info,
71       ],
72     ]);
73     $filename = vfsStream::url('modules/fixtures/broken.info.txt');
74     $this->setExpectedException('\Drupal\Core\Extension\InfoParserException', 'broken.info.txt');
75     $this->infoParser->parse($filename);
76   }
77
78   /**
79    * Tests that missing required keys are detected.
80    *
81    * @covers ::parse
82    */
83   public function testInfoParserMissingKeys() {
84     $missing_keys = <<<MISSINGKEYS
85 # info.yml for testing missing name, description, and type keys.
86 package: Core
87 version: VERSION
88 dependencies:
89   - field
90 MISSINGKEYS;
91
92     vfsStream::setup('modules');
93     vfsStream::create([
94       'fixtures' => [
95         'missing_keys.info.txt' => $missing_keys,
96       ],
97     ]);
98     $filename = vfsStream::url('modules/fixtures/missing_keys.info.txt');
99     $this->setExpectedException('\Drupal\Core\Extension\InfoParserException', 'Missing required keys (type, core, name) in vfs://modules/fixtures/missing_keys.info.txt');
100     $this->infoParser->parse($filename);
101   }
102
103   /**
104    * Tests that missing required key is detected.
105    *
106    * @covers ::parse
107    */
108   public function testInfoParserMissingKey() {
109     $missing_key = <<<MISSINGKEY
110 # info.yml for testing missing type key.
111 name: File
112 description: 'Defines a file field type.'
113 package: Core
114 version: VERSION
115 core: 8.x
116 dependencies:
117   - field
118 MISSINGKEY;
119
120     vfsStream::setup('modules');
121     vfsStream::create([
122       'fixtures' => [
123         'missing_key.info.txt' => $missing_key,
124       ],
125     ]);
126     $filename = vfsStream::url('modules/fixtures/missing_key.info.txt');
127     $this->setExpectedException('\Drupal\Core\Extension\InfoParserException', 'Missing required keys (type) in vfs://modules/fixtures/missing_key.info.txt');
128     $this->infoParser->parse($filename);
129   }
130
131   /**
132    * Tests common info file.
133    *
134    * @covers ::parse
135    */
136   public function testInfoParserCommonInfo() {
137     $common = <<<COMMONTEST
138 core: 8.x
139 name: common_test
140 type: module
141 description: 'testing info file parsing'
142 simple_string: 'A simple string'
143 version: "VERSION"
144 double_colon: dummyClassName::method
145 COMMONTEST;
146
147     vfsStream::setup('modules');
148     vfsStream::create([
149       'fixtures' => [
150         'common_test.info.txt' => $common,
151       ],
152     ]);
153     $info_values = $this->infoParser->parse(vfsStream::url('modules/fixtures/common_test.info.txt'));
154     $this->assertEquals($info_values['simple_string'], 'A simple string', 'Simple string value was parsed correctly.');
155     $this->assertEquals($info_values['version'], \Drupal::VERSION, 'Constant value was parsed correctly.');
156     $this->assertEquals($info_values['double_colon'], 'dummyClassName::method', 'Value containing double-colon was parsed correctly.');
157   }
158
159 }