View source
<?php
namespace Drupal\Tests\ctools\Unit;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Display\VariantInterface;
use Drupal\ctools\Plugin\VariantCollectionTrait;
use Drupal\ctools\Plugin\VariantPluginCollection;
use Drupal\Tests\UnitTestCase;
class VariantCollectionTraitTest extends UnitTestCase {
protected $manager;
protected function setUp() : void {
parent::setUp();
$container = new ContainerBuilder();
$this->manager = $this
->prophesize(PluginManagerInterface::class);
$container
->set('plugin.manager.display_variant', $this->manager
->reveal());
\Drupal::setContainer($container);
}
public function testGetVariantsEmpty() {
$trait_object = new TestVariantCollectionTrait();
$this->manager
->createInstance()
->shouldNotBeCalled();
$variants = $trait_object
->getVariants();
$this
->assertInstanceOf(VariantPluginCollection::class, $variants);
$this
->assertSame(0, count($variants));
}
public function testGetVariants() {
$trait_object = new TestVariantCollectionTrait();
$config = [
'foo' => [
'id' => 'foo_plugin',
],
'bar' => [
'id' => 'bar_plugin',
],
];
foreach ($config as $value) {
$plugin = $this
->prophesize(VariantInterface::class);
$this->manager
->createInstance($value['id'], $value)
->willReturn($plugin
->reveal());
}
$trait_object
->setVariantConfig($config);
$variants = $trait_object
->getVariants();
$this
->assertInstanceOf(VariantPluginCollection::class, $variants);
$this
->assertSame(2, count($variants));
return $variants;
}
public function testGetVariantsSort(VariantPluginCollection $variants) {
$this
->assertEquals([
'bar' => 'bar',
'foo' => 'foo',
], $variants
->getInstanceIds());
}
public function testAddVariant() {
$config = [
'id' => 'foo',
];
$uuid = 'test-uuid';
$expected_config = $config + [
'uuid' => $uuid,
];
$uuid_generator = $this
->prophesize(UuidInterface::class);
$uuid_generator
->generate()
->willReturn($uuid)
->shouldBeCalledTimes(1);
$trait_object = new TestVariantCollectionTrait();
$trait_object
->setUuidGenerator($uuid_generator
->reveal());
$plugin_prophecy = $this
->prophesize(VariantInterface::class);
$plugin_prophecy
->getConfiguration()
->willReturn($expected_config)
->shouldBeCalled();
$plugin_prophecy
->setConfiguration($expected_config)
->willReturn($expected_config)
->shouldBeCalled();
$this->manager
->createInstance('foo', $expected_config)
->willReturn($plugin_prophecy
->reveal());
$resulting_uuid = $trait_object
->addVariant($config);
$this
->assertSame($uuid, $resulting_uuid);
$variants = $trait_object
->getVariants();
$this
->assertSame([
$uuid => $uuid,
], $variants
->getInstanceIds());
$this
->assertSame([
$uuid => $expected_config,
], $variants
->getConfiguration());
$this
->assertSame($plugin_prophecy
->reveal(), $variants
->get($uuid));
return [
$trait_object,
$uuid,
$plugin_prophecy
->reveal(),
];
}
public function testGetVariant($data) {
list($trait_object, $uuid, $plugin) = $data;
$this->manager
->createInstance()
->shouldNotBeCalled();
$this
->assertSame($plugin, $trait_object
->getVariant($uuid));
return [
$trait_object,
$uuid,
];
}
public function testRemoveVariant($data) {
list($trait_object, $uuid) = $data;
$this
->assertSame($trait_object, $trait_object
->removeVariant($uuid));
$this
->assertFalse($trait_object
->getVariants()
->has($uuid));
return [
$trait_object,
$uuid,
];
}
public function testGetVariantException($data) {
list($trait_object, $uuid) = $data;
$this
->expectException('\\Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException');
$this
->expectExceptionMessage("Plugin ID 'test-uuid' was not found.");
$this
->assertNull($trait_object
->getVariant($uuid));
}
}
class TestVariantCollectionTrait {
use VariantCollectionTrait;
protected $variantConfig = [];
protected $uuidGenerator;
public function setUuidGenerator(UuidInterface $uuid_generator) {
$this->uuidGenerator = $uuid_generator;
return $this;
}
protected function uuidGenerator() {
return $this->uuidGenerator;
}
public function setVariantConfig(array $config) {
$this->variantConfig = $config;
return $this;
}
protected function getVariantConfig() {
return $this->variantConfig;
}
}