Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Traits / CreateTestContentEntitiesTrait.php
1 <?php
2
3 namespace Drupal\Tests\migrate_drupal\Traits;
4
5 /**
6  * Provides helper methods for creating test content.
7  */
8 trait CreateTestContentEntitiesTrait {
9
10   /**
11    * Gets required modules.
12    *
13    * @return array
14    */
15   protected function getRequiredModules() {
16     return [
17       'aggregator',
18       'block_content',
19       'comment',
20       'field',
21       'file',
22       'link',
23       'menu_link_content',
24       'migrate_drupal',
25       'node',
26       'options',
27       'system',
28       'taxonomy',
29       'text',
30       'user',
31     ];
32   }
33
34   /**
35    * Install required entity schemas.
36    */
37   protected function installEntitySchemas() {
38     $this->installEntitySchema('aggregator_feed');
39     $this->installEntitySchema('aggregator_item');
40     $this->installEntitySchema('block_content');
41     $this->installEntitySchema('comment');
42     $this->installEntitySchema('file');
43     $this->installEntitySchema('menu_link_content');
44     $this->installEntitySchema('node');
45     $this->installEntitySchema('taxonomy_term');
46     $this->installEntitySchema('user');
47   }
48
49   /**
50    * Create several pieces of generic content.
51    */
52   protected function createContent() {
53     $entity_type_manager = \Drupal::entityTypeManager();
54
55     // Create an aggregator feed.
56     if ($entity_type_manager->hasDefinition('aggregator_feed')) {
57       $feed = $entity_type_manager->getStorage('aggregator_feed')->create([
58         'title' => 'feed',
59         'url' => 'http://www.example.com',
60       ]);
61       $feed->save();
62
63       // Create an aggregator feed item.
64       $item = $entity_type_manager->getStorage('aggregator_item')->create([
65         'title' => 'feed item',
66         'fid' => $feed->id(),
67         'link' => 'http://www.example.com',
68       ]);
69       $item->save();
70     }
71
72     // Create a block content.
73     if ($entity_type_manager->hasDefinition('block_content')) {
74       $block = $entity_type_manager->getStorage('block_content')->create([
75         'info' => 'block',
76         'type' => 'block',
77       ]);
78       $block->save();
79     }
80
81     // Create a node.
82     if ($entity_type_manager->hasDefinition('node')) {
83       $node = $entity_type_manager->getStorage('node')->create([
84         'type' => 'page',
85         'title' => 'page',
86       ]);
87       $node->save();
88
89       // Create a comment.
90       if ($entity_type_manager->hasDefinition('comment')) {
91         $comment = $entity_type_manager->getStorage('comment')->create([
92           'comment_type' => 'comment',
93           'field_name' => 'comment',
94           'entity_type' => 'node',
95           'entity_id' => $node->id(),
96         ]);
97         $comment->save();
98       }
99     }
100
101     // Create a file.
102     if ($entity_type_manager->hasDefinition('file')) {
103       $file = $entity_type_manager->getStorage('file')->create([
104         'uri' => 'public://example.txt',
105       ]);
106       $file->save();
107     }
108
109     // Create a menu link.
110     if ($entity_type_manager->hasDefinition('menu_link_content')) {
111       $menu_link = $entity_type_manager->getStorage('menu_link_content')->create([
112         'title' => 'menu link',
113         'link' => ['uri' => 'http://www.example.com'],
114         'menu_name' => 'tools',
115       ]);
116       $menu_link->save();
117     }
118
119     // Create a taxonomy term.
120     if ($entity_type_manager->hasDefinition('taxonomy_term')) {
121       $term = $entity_type_manager->getStorage('taxonomy_term')->create([
122         'name' => 'term',
123         'vid' => 'term',
124       ]);
125       $term->save();
126     }
127
128     // Create a user.
129     if ($entity_type_manager->hasDefinition('user')) {
130       $user = $entity_type_manager->getStorage('user')->create([
131         'name' => 'user',
132         'mail' => 'user@example.com',
133       ]);
134       $user->save();
135     }
136   }
137
138   /**
139    * Create several pieces of generic content.
140    */
141   protected function createContentPostUpgrade() {
142     $entity_type_manager = \Drupal::entityTypeManager();
143
144     // Create a block content.
145     if ($entity_type_manager->hasDefinition('block_content')) {
146       $block = $entity_type_manager->getStorage('block_content')->create([
147         'info' => 'Post upgrade block',
148         'type' => 'block',
149       ]);
150       $block->save();
151     }
152
153     // Create a node.
154     if ($entity_type_manager->hasDefinition('node')) {
155       $node = $entity_type_manager->getStorage('node')->create([
156         'type' => 'page',
157         'title' => 'Post upgrade page',
158       ]);
159       $node->save();
160
161       // Create a comment.
162       if ($entity_type_manager->hasDefinition('comment')) {
163         $comment = $entity_type_manager->getStorage('comment')->create([
164           'comment_type' => 'comment',
165           'field_name' => 'comment',
166           'entity_type' => 'node',
167           'entity_id' => $node->id(),
168         ]);
169         $comment->save();
170       }
171     }
172
173     // Create a file.
174     if ($entity_type_manager->hasDefinition('file')) {
175       $file = $entity_type_manager->getStorage('file')->create([
176         'uri' => 'public://post_upgrade_example.txt',
177       ]);
178       $file->save();
179     }
180
181     // Create a menu link.
182     if ($entity_type_manager->hasDefinition('menu_link_content')) {
183       $menu_link = $entity_type_manager->getStorage('menu_link_content')->create([
184         'title' => 'post upgrade menu link',
185         'link' => ['uri' => 'http://www.drupal.org'],
186         'menu_name' => 'tools',
187       ]);
188       $menu_link->save();
189     }
190
191     // Create a taxonomy term.
192     if ($entity_type_manager->hasDefinition('taxonomy_term')) {
193       $term = $entity_type_manager->getStorage('taxonomy_term')->create([
194         'name' => 'post upgrade term',
195         'vid' => 'term',
196       ]);
197       $term->save();
198     }
199
200     // Create a user.
201     if ($entity_type_manager->hasDefinition('user')) {
202       $user = $entity_type_manager->getStorage('user')->create([
203         'name' => 'universe',
204         'mail' => 'universe@example.com',
205       ]);
206       $user->save();
207     }
208   }
209
210 }