Pull merge.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Discovery / YamlDiscoveryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Discovery;
4
5 use Drupal\Core\StringTranslation\TranslatableMarkup;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Core\Plugin\Discovery\YamlDiscovery;
8 use org\bovigo\vfs\vfsStream;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Plugin\Discovery\YamlDiscovery
12  * @group Plugin
13  */
14 class YamlDiscoveryTest extends UnitTestCase {
15
16   /**
17    * The YamlDiscovery instance to test.
18    *
19    * @var \Drupal\Core\Plugin\Discovery\YamlDiscovery
20    */
21   protected $discovery;
22
23   /**
24    * Expected provider => key mappings for testing.
25    *
26    * @var array
27    */
28   protected $expectedKeys = [
29     'test_1' => 'test_1_a',
30     'another_provider_1' => 'test_1_b',
31     'another_provider_2' => 'test_2_a',
32     'test_2' => 'test_2_b',
33   ];
34
35   protected function setUp() {
36     parent::setUp();
37
38     $base_path = __DIR__ . '/Fixtures';
39     // Set up the directories to search.
40     $directories = [
41       'test_1' => $base_path . '/test_1',
42       'test_2' => $base_path . '/test_2',
43     ];
44
45     $this->discovery = new YamlDiscovery('test', $directories);
46   }
47
48   /**
49    * Tests the getDefinitions() method.
50    */
51   public function testGetDefinitions() {
52     $definitions = $this->discovery->getDefinitions();
53
54     $this->assertInternalType('array', $definitions);
55     $this->assertCount(4, $definitions);
56
57     foreach ($this->expectedKeys as $expected_key) {
58       $this->assertArrayHasKey($expected_key, $definitions);
59     }
60
61     foreach ($definitions as $id => $definition) {
62       foreach (['name', 'id', 'provider'] as $key) {
63         $this->assertArrayHasKey($key, $definition);
64       }
65       $this->assertEquals($id, $definition['id']);
66       $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
67     }
68   }
69
70   /**
71    * @covers ::getDefinitions
72    */
73   public function testGetDefinitionsWithTranslatableDefinitions() {
74     vfsStream::setup('root');
75
76     $file_1 = <<<'EOS'
77 test_plugin:
78   title: test title
79 EOS;
80     $file_2 = <<<'EOS'
81 test_plugin2:
82   title: test title2
83   title_context: 'test-context'
84 EOS;
85     vfsStream::create([
86       'test_1' => [
87         'test_1.test.yml' => $file_1,
88       ],
89       'test_2' => [
90         'test_2.test.yml' => $file_2,
91       ],
92     ]);
93
94     $discovery = new YamlDiscovery('test', ['test_1' => vfsStream::url('root/test_1'), 'test_2' => vfsStream::url('root/test_2')]);
95     $discovery->addTranslatableProperty('title', 'title_context');
96     $definitions = $discovery->getDefinitions();
97
98     $this->assertCount(2, $definitions);
99     $plugin_1 = $definitions['test_plugin'];
100     $plugin_2 = $definitions['test_plugin2'];
101
102     $this->assertInstanceOf(TranslatableMarkup::class, $plugin_1['title']);
103     $this->assertEquals([], $plugin_1['title']->getOptions());
104     $this->assertInstanceOf(TranslatableMarkup::class, $plugin_2['title']);
105     $this->assertEquals(['context' => 'test-context'], $plugin_2['title']->getOptions());
106   }
107
108   /**
109    * Tests the getDefinition() method.
110    */
111   public function testGetDefinition() {
112     $definitions = $this->discovery->getDefinitions();
113     // Test the getDefinition() method.
114     foreach ($this->expectedKeys as $expected_key) {
115       $this->assertEquals($definitions[$expected_key], $this->discovery->getDefinition($expected_key));
116     }
117   }
118
119 }