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