Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field\Tests\Views;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
6 use Drupal\node\Entity\NodeType;
|
Chris@0
|
7 use Drupal\views\Tests\ViewTestBase;
|
Chris@0
|
8 use Drupal\views\Tests\ViewTestData;
|
Chris@0
|
9 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Provides some helper methods for testing fieldapi integration into views.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @todo Test on a generic entity not on a node. What has to be tested:
|
Chris@0
|
15 * - Make sure that every wanted field is added to the according entity type.
|
Chris@0
|
16 * - Make sure the joins are done correctly.
|
Chris@0
|
17 * - Use basic fields and make sure that the full wanted object is built.
|
Chris@0
|
18 * - Use relationships between different entity types, for example node and
|
Chris@0
|
19 * the node author(user).
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class FieldTestBase extends ViewTestBase {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Modules to enable.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var array
|
Chris@0
|
27 */
|
Chris@0
|
28 public static $modules = ['node', 'field_test_views'];
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Stores the field definitions used by the test.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var array
|
Chris@0
|
34 */
|
Chris@0
|
35 public $fieldStorages;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Stores the fields of the field storage. They have the same keys as the
|
Chris@0
|
39 * field storages.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var array
|
Chris@0
|
42 */
|
Chris@0
|
43 public $fields;
|
Chris@0
|
44
|
Chris@0
|
45 protected function setUp() {
|
Chris@0
|
46 parent::setUp();
|
Chris@0
|
47
|
Chris@0
|
48 // Ensure the page node type exists.
|
Chris@0
|
49 NodeType::create([
|
Chris@0
|
50 'type' => 'page',
|
Chris@0
|
51 'name' => 'page',
|
Chris@0
|
52 ])->save();
|
Chris@0
|
53
|
Chris@0
|
54 ViewTestData::createTestViews(get_class($this), ['field_test_views']);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 public function setUpFieldStorages($amount = 3, $type = 'string') {
|
Chris@0
|
58 // Create three fields.
|
Chris@0
|
59 $field_names = [];
|
Chris@0
|
60 for ($i = 0; $i < $amount; $i++) {
|
Chris@0
|
61 $field_names[$i] = 'field_name_' . $i;
|
Chris@0
|
62 $this->fieldStorages[$i] = FieldStorageConfig::create([
|
Chris@0
|
63 'field_name' => $field_names[$i],
|
Chris@0
|
64 'entity_type' => 'node',
|
Chris@0
|
65 'type' => $type,
|
Chris@0
|
66 ]);
|
Chris@0
|
67 $this->fieldStorages[$i]->save();
|
Chris@0
|
68 }
|
Chris@0
|
69 return $field_names;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 public function setUpFields($bundle = 'page') {
|
Chris@0
|
73 foreach ($this->fieldStorages as $key => $field_storage) {
|
Chris@0
|
74 $this->fields[$key] = FieldConfig::create([
|
Chris@0
|
75 'field_storage' => $field_storage,
|
Chris@0
|
76 'bundle' => $bundle,
|
Chris@0
|
77 ]);
|
Chris@0
|
78 $this->fields[$key]->save();
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 }
|