a334bbd54059411f13afc788ef65956ca86fdabd
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagFieldTestBase.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Base class for ensuring that the Metatag field works correctly.
10  */
11 abstract class MetatagFieldTestBase extends WebTestBase {
12
13   /**
14    * Profile to use.
15    *
16    * @var string
17    */
18   protected $profile = 'testing';
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = [
26     // Needed for token handling.
27     'token',
28
29     // Needed for the field UI testing.
30     'field_ui',
31
32     // Needed to verify that nothing is broken for unsupported entities.
33     'contact',
34
35     // The base module.
36     'metatag',
37
38     // Some extra custom logic for testing Metatag.
39     'metatag_test_tag',
40
41     // Manages the entity type that is being tested.
42     'entity_test',
43   ];
44
45   /**
46    * Admin user
47    *
48    * @var \Drupal\Core\Session\AccountInterface
49    */
50   protected $adminUser;
51
52   /**
53    * Basic permissions that all of the entity tests will need.
54    *
55    * @var array
56    */
57   protected $base_perms = [
58     'access administration pages',
59     'administer meta tags',
60   ];
61
62   /**
63    * Additional permissions needed for this entity type.
64    *
65    * @var array
66    */
67   protected $entity_perms = [];
68
69   /**
70    * The entity type that is being tested.
71    *
72    * @var string
73    */
74   protected $entity_type = '';
75
76   /**
77    * The formal name for this entity type.
78    *
79    * @var string
80    */
81   protected $entity_label = '';
82
83   /**
84    * The entity bundle that is being tested.
85    *
86    * @var string
87    */
88   protected $entity_bundle = '';
89
90   /**
91    * The path to add an object for this entity type.
92    *
93    * @var string
94    */
95   protected $entity_add_path = '';
96
97   /**
98    * The path to access the field admin for this entity bundle.
99    */
100   protected $entity_field_admin_path = '';
101
102   /**
103    * Whether or not this entity type supports default meta tag values.
104    *
105    * @var bool
106    */
107   protected $entity_supports_defaults = TRUE;
108
109   /**
110    * The label used on the entity form for the 'save' action.
111    *
112    * @var string
113    */
114   protected $entity_save_button_label = 'Save';
115
116   /**
117    * The name of the primary title or name field for this entity.
118    *
119    * @var string
120    */
121   protected $entity_title_field = 'title';
122
123   /**
124    * {@inheritDoc}
125    */
126   protected function setUp() {
127     parent::setUp();
128
129     // Any additional configuration that's neede for this entity type.
130     $this->setUpEntityType();
131
132     // Merge the base permissions with the custom ones for the entity type and
133     // create a user with these permissions.
134     $all_perms = array_merge($this->base_perms, $this->entity_perms);
135     $this->adminUser = $this->drupalCreateUser($all_perms);
136     $this->drupalGet('/user/login');
137     $this->assertResponse(200);
138     $this->drupalLogin($this->adminUser);
139   }
140
141   /**
142    * Any additional configuration that's needed for this entity type.
143    */
144   protected function setUpEntityType() {}
145
146   /**
147    * A list of default values to add to the entity being created. If left empty
148    * it will default to "{$entity_title_field}[0][value]" => $title.
149    *
150    * @return array
151    */
152   protected function entity_default_values() {}
153
154   /**
155    * Add a Metatag field to this entity type.
156    */
157   protected function addField() {
158     // Add a metatag field to the entity type test_entity.
159     $this->drupalGet($this->entity_field_admin_path . '/add-field');
160     $this->assertResponse(200);
161     $edit = [
162       'label' => 'Metatag',
163       'field_name' => 'metatag',
164       'new_storage_type' => 'metatag',
165     ];
166     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
167     $this->drupalPostForm(NULL, [], t('Save field settings'));
168
169     // Clear all settings.
170     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
171   }
172
173   /**
174    * Confirm that the global default values work correctly when there are no
175    * entity or bundle defaults available.
176    */
177   public function testGlobalDefaultsInheritance() {
178     // First we set global defaults.
179     $this->drupalGet('admin/config/search/metatag/global');
180     $this->assertResponse(200);
181     $global_values = [
182       'metatag_test' => 'Global description',
183     ];
184     $this->drupalPostForm(NULL, $global_values, 'Save');
185     $this->assertText('Saved the Global Metatag defaults.');
186
187     // Add the field to this entity type.
188     $this->addField();
189
190     // Now when we create an entity, global defaults are used to fill the form
191     // fields.
192     $this->drupalGet($this->entity_add_path);
193     $this->assertResponse(200);
194     $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test'], t('The metatag_test field has the global default as the field default does not define it.'));
195   }
196
197   /**
198    * Confirm that the entity default values work correctly.
199    */
200   public function testEntityDefaultsInheritance() {
201     // This test doesn't make sense if the entity doesn't support defaults.
202     if (!$this->entity_supports_defaults) {
203       return;
204     }
205
206     // Set a global default.
207     $this->drupalGet('admin/config/search/metatag/global');
208     $this->assertResponse(200);
209     $global_values = [
210       'metatag_test' => 'Global description',
211     ];
212     $this->drupalPostForm(NULL, $global_values, 'Save');
213     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
214
215     // Set an entity default.
216     $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
217     $this->assertResponse(200);
218     $entity_values = [
219       'metatag_test' => 'Entity description',
220     ];
221     $this->drupalPostForm(NULL, $entity_values, 'Save');
222     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
223
224     // Add the field to this entity type.
225     $this->addField();
226
227     // Load the entity form for this entity type.
228     $this->drupalGet($this->entity_add_path);
229     $this->assertResponse(200);
230     $this->assertNoText('Fatal error');
231
232     // Allow the fields to be customized if needed.
233     $title = 'Barfoo';
234     $edit = $this->entity_default_values();
235     if (empty($edit)) {
236       $edit = [
237         $this->entity_title_field . '[0][value]' => $title,
238       ];
239     }
240
241     // If this entity type supports defaults then verify the global default is
242     // not present but that the entity default *is* present.
243     $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
244     $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
245   }
246
247   /**
248    * Confirm that the default values for an entity bundle will work correctly
249    * when there is no field for overriding the defaults.
250    */
251   // @todo
252   // public function testBundleDefaultsInheritance() {
253   // }
254
255   /**
256    * Confirm a field can be added to the entity bundle.
257    */
258   public function testFieldCanBeAdded() {
259     $this->drupalGet($this->entity_field_admin_path . '/add-field');
260     $this->assertResponse(200);
261     $this->assertRaw('<option value="metatag">' . t('Meta tags') . '</option>');
262   }
263
264   /**
265    * Confirm a field can be added to the entity bundle.
266    */
267   public function testEntityFieldsAvailable() {
268     // Add a field to the entity type.
269     $this->addField();
270
271     // Load the entity's form.
272     $this->drupalGet($this->entity_add_path);
273     $this->assertResponse(200);
274     $this->assertNoText('Fatal error');
275     $this->assertFieldByName('field_metatag[0][basic][metatag_test]');
276   }
277
278   /**
279    * Confirm that the default values load correctly for an entity created before
280    * the custom field is added.
281    */
282   public function testEntityFieldValuesOldEntity() {
283     // Set a global default.
284     $this->drupalGet('admin/config/search/metatag/global');
285     $this->assertResponse(200);
286     $global_values = [
287       'metatag_test' => 'Global description',
288     ];
289     $this->drupalPostForm(NULL, $global_values, 'Save');
290     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
291
292     // Set an entity default if it's supported by the entity type.
293     if ($this->entity_supports_defaults) {
294       $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
295       $this->assertResponse(200);
296       $entity_values = [
297         'metatag_test' => 'Entity description',
298       ];
299       $this->drupalPostForm(NULL, $entity_values, 'Save');
300       $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
301     }
302
303     // Load the entity form for this entity type.
304     $title = 'Barfoo';
305     $this->drupalGet($this->entity_add_path);
306     $this->assertResponse(200);
307     $this->assertNoText('Fatal error');
308
309     // Allow the fields to be customized if needed.
310     $edit = $this->entity_default_values();
311     if (empty($edit)) {
312       $edit = [
313         $this->entity_title_field . '[0][value]' => $title,
314       ];
315     }
316
317     // Create a new entity object.
318     $this->drupalPostForm(NULL, $edit, t($this->entity_save_button_label));
319     $entities = \Drupal::entityTypeManager()
320       ->getStorage($this->entity_type)
321       ->loadByProperties([$this->entity_title_field => $title]);
322     $this->assertEqual(1, count($entities), 'Entity was saved');
323     $entity = reset($entities);
324
325     // @todo Confirm the values output correctly.
326
327     // Add a field to the entity type.
328     $this->addField();
329
330     // Open the 'edit' form for the entity.
331     $this->drupalGet($entity->toUrl('edit-form'));
332     $this->assertResponse(200);
333
334     // If this entity type supports defaults then verify the global default is
335     // not present but that the entity default *is* present.
336     if ($this->entity_supports_defaults) {
337       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
338       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
339     }
340     else {
341       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
342     }
343
344     // @todo Confirm the values output correctly.
345   }
346
347   /**
348    * Confirm that the default values load correctly for an entity created after
349    * the custom field is added.
350    */
351   public function testEntityFieldValuesNewEntity() {
352     // Set a global default.
353     $this->drupalGet('admin/config/search/metatag/global');
354     $this->assertResponse(200);
355     $global_values = [
356       'metatag_test' => 'Global description',
357     ];
358     $this->drupalPostForm(NULL, $global_values, 'Save');
359     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
360
361     // Set an entity default if it's supported by the entity type.
362     if ($this->entity_supports_defaults) {
363       $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
364       $this->assertResponse(200);
365       $entity_values = [
366         'metatag_test' => 'Entity description',
367       ];
368       $this->drupalPostForm(NULL, $entity_values, 'Save');
369       $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
370     }
371
372     // Add a field to the entity type.
373     $this->addField();
374
375     // Load the entity form for this entity type.
376     $title = 'Barfoo';
377     $this->drupalGet($this->entity_add_path);
378     $this->assertResponse(200);
379     $this->assertNoText('Fatal error');
380
381     // If this entity type supports defaults then verify the global default is
382     // not present but that the entity default *is* present.
383     if ($this->entity_supports_defaults) {
384       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
385       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
386     }
387     else {
388       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
389     }
390
391     // Allow the fields to be customized if needed.
392     $edit = $this->entity_default_values();
393     if (empty($edit)) {
394       $edit = [
395         $this->entity_title_field . '[0][value]' => $title,
396       ];
397     }
398
399     // Create a new entity object.
400     $this->drupalPostForm(NULL, $edit, t($this->entity_save_button_label));
401     $entities = \Drupal::entityTypeManager()
402       ->getStorage($this->entity_type)
403       ->loadByProperties([$this->entity_title_field => $title]);
404     $this->assertEqual(1, count($entities), 'Entity was saved');
405     $entity = reset($entities);
406
407     // @todo Confirm the values output correctly.
408
409     // Open the 'edit' form for the entity.
410     $this->drupalGet($entity->toUrl('edit-form'));
411     $this->assertResponse(200);
412
413     // If this entity type supports defaults then verify the global default is
414     // not present but that the entity default *is* present.
415     if ($this->entity_supports_defaults) {
416       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
417       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
418     }
419     else {
420       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
421     }
422
423     // @todo Confirm the values output correctly.
424   }
425
426   /**
427    * Tests adding and editing values on a given entity type.
428    */
429   public function tofix_testEntityField() {
430     // Add a field to the entity type.
431     $this->addField();
432
433     // Create a test entity.
434     $this->drupalGet($this->entity_add_path);
435     $this->assertResponse(200);
436     $this->assertNoText('Fatal error');
437     $edit = $this->entity_default_values($title) + [
438       'field_metatag[0][basic][metatag_test]' => 'Kilimanjaro',
439     ];
440     $this->drupalPostForm(NULL, $edit, t('Save'));
441     $entities = \Drupal::entityTypeManager()
442       ->getStorage('entity_test')
443       ->loadByProperties([$this->entity_title_field => 'Barfoo']);
444     $this->assertEqual(1, count($entities), 'Entity was saved');
445     $entity = reset($entities);
446
447     // Make sure tags that have a field value but no default value still show
448     // up.
449     $this->drupalGet($entity->toUrl());
450     $this->assertResponse(200);
451     $elements = $this->cssSelect('meta[name=metatag_test]');
452     $this->assertTrue(count($elements) === 1, 'Found keywords metatag_test from defaults');
453     $this->assertEqual((string) $elements[0]['content'], 'Kilimanjaro', 'Field value for metatag_test found when no default set.');
454
455     // @TODO: This should not be required, but metatags does not invalidate
456     // cache upon setting globals.
457     Cache::invalidateTags(['entity_test:' . $entity->id()]);
458
459     // Update the Global defaults and test them.
460     $this->drupalGet('admin/config/search/metatag/global');
461     $this->assertResponse(200);
462     $values = [
463       'metatag_test' => 'Purple monkey dishwasher',
464     ];
465     $this->drupalPostForm(NULL, $values, 'Save');
466     $this->assertText('Saved the Global Metatag defaults.');
467     $this->drupalGet($entity->toUrl());
468     $this->assertResponse(200);
469     $elements = $this->cssSelect('meta[name=metatag_test]');
470     $this->assertTrue(count($elements) === 1, 'Found test metatag from defaults');
471     $this->verbose('<pre>' . print_r($elements, TRUE) . '</pre>');
472     $this->assertEqual((string) $elements[0]['content'], $values['metatag_test']);//, 'Default metatag_test value found.');
473   }
474
475 }