9f53e63313fa75e64e38633ef2a17194698eebc0
[yaffs-website] / web / core / modules / options / tests / src / Kernel / Views / OptionsTestBase.php
1 <?php
2
3 namespace Drupal\Tests\options\Kernel\Views;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\views\Tests\ViewTestData;
11
12 /**
13  * Base class for options views tests.
14  */
15 abstract class OptionsTestBase extends ViewsKernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['options', 'options_test_views', 'node', 'user', 'field'];
23
24   /**
25    * Stores the nodes used for the different tests.
26    *
27    * @var array
28    */
29   protected $nodes = [];
30
31   /**
32    * Stores the field values used for the different tests.
33    *
34    * @var array
35    */
36   protected $fieldValues = [];
37
38   /**
39    * The used field names.
40    *
41    * @var string[]
42    */
43   protected $fieldNames;
44
45   protected function setUp($import_test_views = TRUE) {
46     parent::setUp();
47     $this->mockStandardInstall();
48
49     ViewTestData::createTestViews(get_class($this), ['options_test_views']);
50
51     $settings = [];
52     $settings['type'] = 'article';
53     $settings['title'] = $this->randomString();
54     $settings['field_test_list_string'][]['value'] = $this->fieldValues[0];
55     $settings['field_test_list_integer'][]['value'] = 0;
56
57     $node = Node::create($settings);
58     $node->save();
59
60     $this->nodes[] = $node;
61     $node = $node->createDuplicate();
62     $node->save();
63     $this->nodes[] = $node;
64   }
65
66   /**
67    * Provides a workaround for the inability to use the standard profile.
68    *
69    * @see https://www.drupal.org/node/1708692
70    */
71   protected function mockStandardInstall() {
72     $this->installEntitySchema('user');
73     $this->installEntitySchema('node');
74
75     NodeType::create(
76       ['type' => 'article']
77     )->save();
78     $this->fieldValues = [
79       $this->randomMachineName(),
80       $this->randomMachineName(),
81     ];
82
83     $this->fieldNames = ['field_test_list_string', 'field_test_list_integer'];
84
85     // Create two field entities.
86     FieldStorageConfig::create([
87       'field_name' => $this->fieldNames[0],
88       'entity_type' => 'node',
89       'type' => 'list_string',
90       'cardinality' => 1,
91       'settings' => [
92         'allowed_values' => [
93           $this->fieldValues[0] => $this->fieldValues[0],
94           $this->fieldValues[1] => $this->fieldValues[1],
95         ],
96       ],
97     ])->save();
98     FieldStorageConfig::create([
99       'field_name' => $this->fieldNames[1],
100       'entity_type' => 'node',
101       'type' => 'list_integer',
102       'cardinality' => 1,
103       'settings' => [
104         'allowed_values' => [
105           $this->fieldValues[0],
106           $this->fieldValues[1],
107         ],
108       ],
109     ])->save();
110     foreach ($this->fieldNames as $field_name) {
111       FieldConfig::create([
112         'field_name' => $field_name,
113         'entity_type' => 'node',
114         'label' => 'Test options list field',
115         'bundle' => 'article',
116       ])->save();
117     }
118   }
119
120 }