d42c1567e3c455daadce82ef5f0f307ff2892837
[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->drupalLogin($this->adminUser);
137   }
138
139   /**
140    * Any additional configuration that's needed for this entity type.
141    */
142   protected function setUpEntityType() {}
143
144   /**
145    * A list of default values to add to the entity being created. If left empty
146    * it will default to "{$entity_title_field}[0][value]" => $title.
147    *
148    * @return array
149    */
150   protected function entity_default_values() {}
151
152   /**
153    * Add a Metatag field to this entity type.
154    */
155   protected function addField() {
156     // Add a metatag field to the entity type test_entity.
157     $this->drupalGet($this->entity_field_admin_path . '/add-field');
158     $this->assertResponse(200);
159     $edit = [
160       'label' => 'Metatag',
161       'field_name' => 'metatag',
162       'new_storage_type' => 'metatag',
163     ];
164     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
165     $this->drupalPostForm(NULL, [], t('Save field settings'));
166
167     // Clear all settings.
168     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
169   }
170
171   /**
172    * Confirm that the global default values work correctly when there are no
173    * entity or bundle defaults available.
174    */
175   public function testGlobalDefaultsInheritance() {
176     // First we set global defaults.
177     $this->drupalGet('admin/config/search/metatag/global');
178     $this->assertResponse(200);
179     $global_values = [
180       'metatag_test' => 'Global description',
181     ];
182     $this->drupalPostForm(NULL, $global_values, 'Save');
183     $this->assertText('Saved the Global Metatag defaults.');
184
185     // Add the field to this entity type.
186     $this->addField();
187
188     // Now when we create an entity, global defaults are used to fill the form
189     // fields.
190     $this->drupalGet($this->entity_add_path);
191     $this->assertResponse(200);
192     $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.'));
193   }
194
195   /**
196    * Confirm that the entity default values work correctly.
197    */
198   public function testEntityDefaultsInheritance() {
199     // This test doesn't make sense if the entity doesn't support defaults.
200     if (!$this->entity_supports_defaults) {
201       return;
202     }
203
204     // Set a global default.
205     $this->drupalGet('admin/config/search/metatag/global');
206     $this->assertResponse(200);
207     $global_values = [
208       'metatag_test' => 'Global description',
209     ];
210     $this->drupalPostForm(NULL, $global_values, 'Save');
211     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
212
213     // Set an entity default.
214     $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
215     $this->assertResponse(200);
216     $entity_values = [
217       'metatag_test' => 'Entity description',
218     ];
219     $this->drupalPostForm(NULL, $entity_values, 'Save');
220     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
221
222     // Add the field to this entity type.
223     $this->addField();
224
225     // Load the entity form for this entity type.
226     $this->drupalGet($this->entity_add_path);
227     $this->assertResponse(200);
228     $this->assertNoText('Fatal error');
229
230     // Allow the fields to be customized if needed.
231     $title = 'Barfoo';
232     $edit = $this->entity_default_values();
233     if (empty($edit)) {
234       $edit = [
235         $this->entity_title_field . '[0][value]' => $title,
236       ];
237     }
238
239     // If this entity type supports defaults then verify the global default is
240     // not present but that the entity default *is* present.
241     $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
242     $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
243   }
244
245   /**
246    * Confirm that the default values for an entity bundle will work correctly
247    * when there is no field for overriding the defaults.
248    */
249   // @todo
250   // public function testBundleDefaultsInheritance() {
251   // }
252
253   /**
254    * Confirm a field can be added to the entity bundle.
255    */
256   public function testFieldCanBeAdded() {
257     $this->drupalGet($this->entity_field_admin_path . '/add-field');
258     $this->assertResponse(200);
259     $this->assertRaw('<option value="metatag">' . t('Meta tags') . '</option>');
260   }
261
262   /**
263    * Confirm a field can be added to the entity bundle.
264    */
265   public function testEntityFieldsAvailable() {
266     // Add a field to the entity type.
267     $this->addField();
268
269     // Load the entity's form.
270     $this->drupalGet($this->entity_add_path);
271     $this->assertResponse(200);
272     $this->assertNoText('Fatal error');
273     $this->assertFieldByName('field_metatag[0][basic][metatag_test]');
274   }
275
276   /**
277    * Confirm that the default values load correctly for an entity created before
278    * the custom field is added.
279    */
280   public function testEntityFieldValuesOldEntity() {
281     // Set a global default.
282     $this->drupalGet('admin/config/search/metatag/global');
283     $this->assertResponse(200);
284     $global_values = [
285       'metatag_test' => 'Global description',
286     ];
287     $this->drupalPostForm(NULL, $global_values, 'Save');
288     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
289
290     // Set an entity default if it's supported by the entity type.
291     if ($this->entity_supports_defaults) {
292       $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
293       $this->assertResponse(200);
294       $entity_values = [
295         'metatag_test' => 'Entity description',
296       ];
297       $this->drupalPostForm(NULL, $entity_values, 'Save');
298       $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
299     }
300
301     // Load the entity form for this entity type.
302     $title = 'Barfoo';
303     $this->drupalGet($this->entity_add_path);
304     $this->assertResponse(200);
305     $this->assertNoText('Fatal error');
306
307     // Allow the fields to be customized if needed.
308     $edit = $this->entity_default_values();
309     if (empty($edit)) {
310       $edit = [
311         $this->entity_title_field . '[0][value]' => $title,
312       ];
313     }
314
315     // Create a new entity object.
316     $this->drupalPostForm(NULL, $edit, t($this->entity_save_button_label));
317     $entities = entity_load_multiple_by_properties($this->entity_type, [
318       $this->entity_title_field => $title,
319     ]);
320     $this->assertEqual(1, count($entities), 'Entity was saved');
321     $entity = reset($entities);
322
323     // @todo Confirm the values output correctly.
324
325     // Add a field to the entity type.
326     $this->addField();
327
328     // Open the 'edit' form for the entity.
329     $this->drupalGet($entity->toUrl('edit-form'));
330     $this->assertResponse(200);
331
332     // If this entity type supports defaults then verify the global default is
333     // not present but that the entity default *is* present.
334     if ($this->entity_supports_defaults) {
335       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
336       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
337     }
338     else {
339       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
340     }
341
342     // @todo Confirm the values output correctly.
343   }
344
345   /**
346    * Confirm that the default values load correctly for an entity created after
347    * the custom field is added.
348    */
349   public function testEntityFieldValuesNewEntity() {
350     // Set a global default.
351     $this->drupalGet('admin/config/search/metatag/global');
352     $this->assertResponse(200);
353     $global_values = [
354       'metatag_test' => 'Global description',
355     ];
356     $this->drupalPostForm(NULL, $global_values, 'Save');
357     $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
358
359     // Set an entity default if it's supported by the entity type.
360     if ($this->entity_supports_defaults) {
361       $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
362       $this->assertResponse(200);
363       $entity_values = [
364         'metatag_test' => 'Entity description',
365       ];
366       $this->drupalPostForm(NULL, $entity_values, 'Save');
367       $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
368     }
369
370     // Add a field to the entity type.
371     $this->addField();
372
373     // Load the entity form for this entity type.
374     $title = 'Barfoo';
375     $this->drupalGet($this->entity_add_path);
376     $this->assertResponse(200);
377     $this->assertNoText('Fatal error');
378
379     // If this entity type supports defaults then verify the global default is
380     // not present but that the entity default *is* present.
381     if ($this->entity_supports_defaults) {
382       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
383       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
384     }
385     else {
386       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
387     }
388
389     // Allow the fields to be customized if needed.
390     $edit = $this->entity_default_values();
391     if (empty($edit)) {
392       $edit = [
393         $this->entity_title_field . '[0][value]' => $title,
394       ];
395     }
396
397     // Create a new entity object.
398     $this->drupalPostForm(NULL, $edit, t($this->entity_save_button_label));
399     $entities = entity_load_multiple_by_properties($this->entity_type, [
400       $this->entity_title_field => $title,
401     ]);
402     $this->assertEqual(1, count($entities), 'Entity was saved');
403     $entity = reset($entities);
404
405     // @todo Confirm the values output correctly.
406
407     // Open the 'edit' form for the entity.
408     $this->drupalGet($entity->toUrl('edit-form'));
409     $this->assertResponse(200);
410
411     // If this entity type supports defaults then verify the global default is
412     // not present but that the entity default *is* present.
413     if ($this->entity_supports_defaults) {
414       $this->assertNoFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
415       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $entity_values['metatag_test']);
416     }
417     else {
418       $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
419     }
420
421     // @todo Confirm the values output correctly.
422   }
423
424   /**
425    * Tests adding and editing values on a given entity type.
426    */
427   public function tofix_testEntityField() {
428     // Add a field to the entity type.
429     $this->addField();
430
431     // Create a test entity.
432     $this->drupalGet($this->entity_add_path);
433     $this->assertResponse(200);
434     $this->assertNoText('Fatal error');
435     $edit = $this->entity_default_values($title) + [
436       'field_metatag[0][basic][metatag_test]' => 'Kilimanjaro',
437     ];
438     $this->drupalPostForm(NULL, $edit, t('Save'));
439     $entities = entity_load_multiple_by_properties('entity_test', [
440       $this->entity_title_field => 'Barfoo',
441     ]);
442     $this->assertEqual(1, count($entities), 'Entity was saved');
443     $entity = reset($entities);
444
445     // Make sure tags that have a field value but no default value still show
446     // up.
447     $this->drupalGet($entity->toUrl());
448     $this->assertResponse(200);
449     $elements = $this->cssSelect('meta[name=metatag_test]');
450     $this->assertTrue(count($elements) === 1, 'Found keywords metatag_test from defaults');
451     $this->assertEqual((string) $elements[0]['content'], 'Kilimanjaro', 'Field value for metatag_test found when no default set.');
452
453     // @TODO: This should not be required, but metatags does not invalidate
454     // cache upon setting globals.
455     Cache::invalidateTags(['entity_test:' . $entity->id()]);
456
457     // Update the Global defaults and test them.
458     $this->drupalGet('admin/config/search/metatag/global');
459     $this->assertResponse(200);
460     $values = [
461       'metatag_test' => 'Purple monkey dishwasher',
462     ];
463     $this->drupalPostForm(NULL, $values, 'Save');
464     $this->assertText('Saved the Global Metatag defaults.');
465     $this->drupalGet($entity->toUrl());
466     $this->assertResponse(200);
467     $elements = $this->cssSelect('meta[name=metatag_test]');
468     $this->assertTrue(count($elements) === 1, 'Found test metatag from defaults');
469     $this->verbose('<pre>' . print_r($elements, TRUE) . '</pre>');
470     $this->assertEqual((string) $elements[0]['content'], $values['metatag_test']);//, 'Default metatag_test value found.');
471   }
472
473 }