3 namespace Drupal\Tests\Core\Plugin\Discovery;
5 use Drupal\Core\StringTranslation\TranslatableMarkup;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Core\Plugin\Discovery\YamlDiscovery;
8 use org\bovigo\vfs\vfsStream;
11 * @coversDefaultClass \Drupal\Core\Plugin\Discovery\YamlDiscovery
14 class YamlDiscoveryTest extends UnitTestCase {
17 * The YamlDiscovery instance to test.
19 * @var \Drupal\Core\Plugin\Discovery\YamlDiscovery
24 * Expected provider => key mappings for testing.
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',
35 protected function setUp() {
38 $base_path = __DIR__ . '/Fixtures';
39 // Set up the directories to search.
41 'test_1' => $base_path . '/test_1',
42 'test_2' => $base_path . '/test_2',
45 $this->discovery = new YamlDiscovery('test', $directories);
49 * Tests the getDefinitions() method.
51 public function testGetDefinitions() {
52 $definitions = $this->discovery->getDefinitions();
54 $this->assertInternalType('array', $definitions);
55 $this->assertCount(4, $definitions);
57 foreach ($this->expectedKeys as $expected_key) {
58 $this->assertArrayHasKey($expected_key, $definitions);
61 foreach ($definitions as $id => $definition) {
62 foreach (['name', 'id', 'provider'] as $key) {
63 $this->assertArrayHasKey($key, $definition);
65 $this->assertEquals($id, $definition['id']);
66 $this->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
71 * @covers ::getDefinitions
73 public function testGetDefinitionsWithTranslatableDefinitions() {
74 vfsStream::setup('root');
83 title_context: 'test-context'
87 'test_1.test.yml' => $file_1,
90 'test_2.test.yml' => $file_2,
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();
98 $this->assertCount(2, $definitions);
99 $plugin_1 = $definitions['test_plugin'];
100 $plugin_2 = $definitions['test_plugin2'];
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());
109 * Tests the getDefinition() method.
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));