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