Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Display / DisplayVariantTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Display;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Display\VariantBase
10  * @group Display
11  */
12 class DisplayVariantTest extends UnitTestCase {
13
14   /**
15    * Sets up a display variant plugin for testing.
16    *
17    * @param array $configuration
18    *   An array of plugin configuration.
19    * @param array $definition
20    *   The plugin definition array.
21    *
22    * @return \Drupal\Core\Display\VariantBase|\PHPUnit_Framework_MockObject_MockObject
23    *   A mocked display variant plugin.
24    */
25   public function setUpDisplayVariant($configuration = [], $definition = []) {
26     return $this->getMockBuilder('Drupal\Core\Display\VariantBase')
27       ->setConstructorArgs([$configuration, 'test', $definition])
28       ->setMethods(['build'])
29       ->getMock();
30   }
31
32   /**
33    * Tests the label() method.
34    *
35    * @covers ::label
36    */
37   public function testLabel() {
38     $display_variant = $this->setUpDisplayVariant(['label' => 'foo']);
39     $this->assertSame('foo', $display_variant->label());
40   }
41
42   /**
43    * Tests the label() method using a default value.
44    *
45    * @covers ::label
46    */
47   public function testLabelDefault() {
48     $display_variant = $this->setUpDisplayVariant();
49     $this->assertSame('', $display_variant->label());
50   }
51
52   /**
53    * Tests the getWeight() method.
54    *
55    * @covers ::getWeight
56    */
57   public function testGetWeight() {
58     $display_variant = $this->setUpDisplayVariant(['weight' => 5]);
59     $this->assertSame(5, $display_variant->getWeight());
60   }
61
62   /**
63    * Tests the getWeight() method using a default value.
64    *
65    * @covers ::getWeight
66    */
67   public function testGetWeightDefault() {
68     $display_variant = $this->setUpDisplayVariant();
69     $this->assertSame(0, $display_variant->getWeight());
70   }
71
72   /**
73    * Tests the getConfiguration() method.
74    *
75    * @covers ::getConfiguration
76    *
77    * @dataProvider providerTestGetConfiguration
78    */
79   public function testGetConfiguration($configuration, $expected) {
80     $display_variant = $this->setUpDisplayVariant($configuration);
81
82     $this->assertSame($expected, $display_variant->getConfiguration());
83   }
84
85   /**
86    * Provides test data for testGetConfiguration().
87    */
88   public function providerTestGetConfiguration() {
89     $data = [];
90     $data[] = [
91       [],
92       [
93         'id' => 'test',
94         'label' => '',
95         'uuid' => '',
96         'weight' => 0,
97       ],
98     ];
99     $data[] = [
100       ['label' => 'Test'],
101       [
102         'id' => 'test',
103         'label' => 'Test',
104         'uuid' => '',
105         'weight' => 0,
106       ],
107     ];
108     $data[] = [
109       ['id' => 'foo'],
110       [
111         'id' => 'test',
112         'label' => '',
113         'uuid' => '',
114         'weight' => 0,
115       ],
116     ];
117     return $data;
118   }
119
120   /**
121    * Tests the access() method.
122    *
123    * @covers ::access
124    */
125   public function testAccess() {
126     $display_variant = $this->setUpDisplayVariant();
127     $this->assertTrue($display_variant->access());
128   }
129
130   /**
131    * Tests the submitConfigurationForm() method.
132    *
133    * @covers ::submitConfigurationForm
134    */
135   public function testSubmitConfigurationForm() {
136     $display_variant = $this->setUpDisplayVariant();
137     $this->assertSame('', $display_variant->label());
138
139     $form = [];
140     $label = $this->randomMachineName();
141     $form_state = new FormState();
142     $form_state->setValue('label', $label);
143     $display_variant->submitConfigurationForm($form, $form_state);
144     $this->assertSame($label, $display_variant->label());
145   }
146
147 }