From e8e84d70736fb488e6ed12ec1dd5b8dd27120571 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sun, 12 Apr 2015 11:20:19 +0100 Subject: [PATCH] Issue #2331407 by Xano, tstoeckler, damiankloip: YamlDiscovery does not handle empty files --- .../Component/Discovery/YamlDiscovery.php | 4 ++- .../Discovery/Fixtures/test_1/test_1.test.yml | 1 - .../Discovery/Fixtures/test_2/test_2.test.yml | 1 - .../Discovery/Fixtures/test_2/test_3.test.yml | 1 - .../Component/Discovery/YamlDiscoveryTest.php | 35 ++++++++++++++----- 5 files changed, 30 insertions(+), 12 deletions(-) delete mode 100644 core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml delete mode 100644 core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml delete mode 100644 core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml diff --git a/core/lib/Drupal/Component/Discovery/YamlDiscovery.php b/core/lib/Drupal/Component/Discovery/YamlDiscovery.php index bf4ee15aa778..1d147fd08ba1 100644 --- a/core/lib/Drupal/Component/Discovery/YamlDiscovery.php +++ b/core/lib/Drupal/Component/Discovery/YamlDiscovery.php @@ -48,7 +48,9 @@ public function __construct($name, array $directories) { public function findAll() { $all = array(); foreach ($this->findFiles() as $provider => $file) { - $all[$provider] = Yaml::decode(file_get_contents($file)); + // If a file is empty or its contents are commented out, return an empty + // array instead of NULL for type consistency. + $all[$provider] = Yaml::decode(file_get_contents($file)) ?: []; } return $all; diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml deleted file mode 100644 index b32753625170..000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml deleted file mode 100644 index b32753625170..000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml deleted file mode 100644 index b32753625170..000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php b/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php index a5469922a98f..1849cd53a2ef 100644 --- a/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php @@ -9,6 +9,9 @@ use Drupal\Tests\UnitTestCase; use Drupal\Component\Discovery\YamlDiscovery; +use org\bovigo\vfs\vfsStream; +use org\bovigo\vfs\vfsStreamWrapper; +use org\bovigo\vfs\vfsStreamDirectory; /** * YamlDiscovery component unit tests. @@ -21,13 +24,26 @@ class YamlDiscoveryTest extends UnitTestCase { * Tests the YAML file discovery. */ public function testDiscovery() { - $base_path = __DIR__ . '/Fixtures'; + vfsStreamWrapper::register(); + $root = new vfsStreamDirectory('modules'); + vfsStreamWrapper::setRoot($root); + $url = vfsStream::url('modules'); + + mkdir($url . '/test_1'); + file_put_contents($url . '/test_1/test_1.test.yml', 'name: test'); + file_put_contents($url . '/test_1/test_2.test.yml', 'name: test'); + + mkdir($url . '/test_2'); + file_put_contents($url . '/test_2/test_3.test.yml', 'name: test'); + // Write an empty YAML file. + file_put_contents($url . '/test_2/test_4.test.yml', ''); + // Set up the directories to search. $directories = array( - 'test_1' => $base_path . '/test_1', - 'test_2' => $base_path . '/test_2', - // Use the same directory with a different provider name. - 'test_3' => $base_path . '/test_2', + 'test_1' => $url . '/test_1', + 'test_2' => $url . '/test_1', + 'test_3' => $url . '/test_2', + 'test_4' => $url . '/test_2', ); $discovery = new YamlDiscovery('test', $directories); @@ -37,11 +53,14 @@ public function testDiscovery() { $this->assertArrayHasKey('test_1', $data); $this->assertArrayHasKey('test_2', $data); $this->assertArrayHasKey('test_3', $data); + $this->assertArrayHasKey('test_4', $data); - foreach ($data as $item) { - $this->assertArrayHasKey('name', $item); - $this->assertEquals($item['name'], 'test'); + foreach (array('test_1', 'test_2', 'test_3') as $key) { + $this->assertArrayHasKey('name', $data[$key]); + $this->assertEquals($data[$key]['name'], 'test'); } + + $this->assertSame(array(), $data['test_4']); } } -- GitLab