3 namespace Drupal\Tests\Core\Extension;
5 use Drupal\Core\Extension\InfoParser;
6 use Drupal\Tests\UnitTestCase;
7 use org\bovigo\vfs\vfsStream;
10 * Tests InfoParser class and exception.
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
16 * @coversDefaultClass \Drupal\Core\Extension\InfoParser
20 class InfoParserUnitTest extends UnitTestCase {
23 * The InfoParser object.
25 * @var \Drupal\Core\Extension\InfoParser
27 protected $infoParser;
32 protected function setUp() {
35 $this->infoParser = new InfoParser();
39 * Tests the functionality of the infoParser object.
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.');
50 * Test if correct exception is thrown for a broken info file.
54 public function testInfoParserBroken() {
55 $broken_info = <<<BROKEN_INFO
56 # info.yml for testing broken YAML parsing exception handling.
59 description: 'Defines a file field type.'
67 vfsStream::setup('modules');
70 'broken.info.txt' => $broken_info,
73 $filename = vfsStream::url('modules/fixtures/broken.info.txt');
74 $this->setExpectedException('\Drupal\Core\Extension\InfoParserException', 'broken.info.txt');
75 $this->infoParser->parse($filename);
79 * Tests that missing required keys are detected.
83 public function testInfoParserMissingKeys() {
84 $missing_keys = <<<MISSINGKEYS
85 # info.yml for testing missing name, description, and type keys.
92 vfsStream::setup('modules');
95 'missing_keys.info.txt' => $missing_keys,
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);
104 * Tests that missing required key is detected.
108 public function testInfoParserMissingKey() {
109 $missing_key = <<<MISSINGKEY
110 # info.yml for testing missing type key.
112 description: 'Defines a file field type.'
120 vfsStream::setup('modules');
123 'missing_key.info.txt' => $missing_key,
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);
132 * Tests common info file.
136 public function testInfoParserCommonInfo() {
137 $common = <<<COMMONTEST
141 description: 'testing info file parsing'
142 simple_string: 'A simple string'
144 double_colon: dummyClassName::method
147 vfsStream::setup('modules');
150 'common_test.info.txt' => $common,
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.');