8926879435426f7674e703c95f16456b76756043
[yaffs-website] / web / modules / contrib / ctools / tests / src / Unit / BlockVariantTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\ctools\Unit;
4
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\ctools\Plugin\BlockPluginCollection;
7 use Drupal\ctools\Plugin\BlockVariantTrait;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * Tests the methods of a block-based variant.
12  *
13  * @coversDefaultClass \Drupal\ctools\Plugin\BlockVariantTrait
14  *
15  * @group CTools
16  */
17 class BlockVariantTraitTest extends UnitTestCase {
18
19   /**
20    * Tests the getRegionAssignments() method.
21    *
22    * @covers ::getRegionAssignments
23    *
24    * @dataProvider providerTestGetRegionAssignments
25    */
26   public function testGetRegionAssignments($expected, $blocks = []) {
27     $block_collection = $this->prophesize(BlockPluginCollection::class);
28     $block_collection->getAllByRegion()
29       ->willReturn($blocks)
30       ->shouldBeCalled();
31
32     $display_variant = new TestBlockVariantTrait();
33     $display_variant->setBlockPluginCollection($block_collection->reveal());
34
35     $this->assertSame($expected, $display_variant->getRegionAssignments());
36   }
37
38   public function providerTestGetRegionAssignments() {
39     return [
40       [
41         [
42           'top' => [],
43           'bottom' => [],
44         ],
45       ],
46       [
47         [
48           'top' => ['foo'],
49           'bottom' => [],
50         ],
51         [
52           'top' => ['foo'],
53         ],
54       ],
55       [
56         [
57           'top' => [],
58           'bottom' => [],
59         ],
60         [
61           'invalid' => ['foo'],
62         ],
63       ],
64       [
65         [
66           'top' => [],
67           'bottom' => ['foo'],
68         ],
69         [
70           'bottom' => ['foo'],
71           'invalid' => ['bar'],
72         ],
73       ],
74     ];
75   }
76
77 }
78
79 class TestBlockVariantTrait {
80   use BlockVariantTrait;
81
82   /**
83    * @var array
84    */
85   protected $blockConfig = [];
86
87   /**
88    * @var \Drupal\Component\Uuid\UuidInterface
89    */
90   protected $uuidGenerator;
91
92   /**
93    * @param BlockPluginCollection $block_plugin_collection
94    *
95    * @return $this
96    */
97   public function setBlockPluginCollection(BlockPluginCollection $block_plugin_collection) {
98     $this->blockPluginCollection = $block_plugin_collection;
99     return $this;
100   }
101
102   /**
103    * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
104    *
105    * @return $this
106    */
107   public function setUuidGenerator(UuidInterface $uuid_generator) {
108     $this->uuidGenerator = $uuid_generator;
109     return $this;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   protected function uuidGenerator() {
116     return $this->uuidGenerator;
117   }
118
119   /**
120    * Sets the block configuration.
121    *
122    * @param array $config
123    *   The block configuration.
124    *
125    * @return $this
126    */
127   public function setBlockConfig(array $config) {
128     $this->blockConfig = $config;
129     return $this;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   protected function getBlockConfig() {
136     return $this->blockConfig;
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function getRegionNames() {
143     return [
144       'top' => 'Top',
145       'bottom' => 'Bottom',
146     ];
147   }
148
149 }