3 namespace Drupal\metatag\Tests;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\simpletest\WebTestBase;
9 * Base class for ensuring that the Metatag field works correctly.
11 abstract class MetatagFieldTestBase extends WebTestBase {
18 protected $profile = 'testing';
25 public static $modules = [
26 // Needed for token handling.
29 // Needed for the field UI testing.
32 // Needed to verify that nothing is broken for unsupported entities.
38 // Some extra custom logic for testing Metatag.
41 // Manages the entity type that is being tested.
48 * @var \Drupal\Core\Session\AccountInterface
53 * Basic permissions that all of the entity tests will need.
57 protected $base_perms = [
58 'access administration pages',
59 'administer meta tags',
63 * Additional permissions needed for this entity type.
67 protected $entity_perms = [];
70 * The entity type that is being tested.
74 protected $entity_type = '';
77 * The formal name for this entity type.
81 protected $entity_label = '';
84 * The entity bundle that is being tested.
88 protected $entity_bundle = '';
91 * The path to add an object for this entity type.
95 protected $entity_add_path = '';
98 * The path to access the field admin for this entity bundle.
100 protected $entity_field_admin_path = '';
103 * Whether or not this entity type supports default meta tag values.
107 protected $entity_supports_defaults = TRUE;
110 * The label used on the entity form for the 'save' action.
114 protected $entity_save_button_label = 'Save';
117 * The name of the primary title or name field for this entity.
121 protected $entity_title_field = 'title';
126 protected function setUp() {
129 // Any additional configuration that's neede for this entity type.
130 $this->setUpEntityType();
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);
142 * Any additional configuration that's needed for this entity type.
144 protected function setUpEntityType() {}
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.
152 protected function entity_default_values() {}
155 * Add a Metatag field to this entity type.
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);
162 'label' => 'Metatag',
163 'field_name' => 'metatag',
164 'new_storage_type' => 'metatag',
166 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
167 $this->drupalPostForm(NULL, [], t('Save field settings'));
169 // Clear all settings.
170 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
174 * Confirm that the global default values work correctly when there are no
175 * entity or bundle defaults available.
177 public function testGlobalDefaultsInheritance() {
178 // First we set global defaults.
179 $this->drupalGet('admin/config/search/metatag/global');
180 $this->assertResponse(200);
182 'metatag_test' => 'Global description',
184 $this->drupalPostForm(NULL, $global_values, 'Save');
185 $this->assertText('Saved the Global Metatag defaults.');
187 // Add the field to this entity type.
190 // Now when we create an entity, global defaults are used to fill the form
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.'));
198 * Confirm that the entity default values work correctly.
200 public function testEntityDefaultsInheritance() {
201 // This test doesn't make sense if the entity doesn't support defaults.
202 if (!$this->entity_supports_defaults) {
206 // Set a global default.
207 $this->drupalGet('admin/config/search/metatag/global');
208 $this->assertResponse(200);
210 'metatag_test' => 'Global description',
212 $this->drupalPostForm(NULL, $global_values, 'Save');
213 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
215 // Set an entity default.
216 $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
217 $this->assertResponse(200);
219 'metatag_test' => 'Entity description',
221 $this->drupalPostForm(NULL, $entity_values, 'Save');
222 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
224 // Add the field to this entity type.
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');
232 // Allow the fields to be customized if needed.
234 $edit = $this->entity_default_values();
237 $this->entity_title_field . '[0][value]' => $title,
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']);
248 * Confirm that the default values for an entity bundle will work correctly
249 * when there is no field for overriding the defaults.
252 // public function testBundleDefaultsInheritance() {
256 * Confirm a field can be added to the entity bundle.
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>');
265 * Confirm a field can be added to the entity bundle.
267 public function testEntityFieldsAvailable() {
268 // Add a field to the entity type.
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]');
279 * Confirm that the default values load correctly for an entity created before
280 * the custom field is added.
282 public function testEntityFieldValuesOldEntity() {
283 // Set a global default.
284 $this->drupalGet('admin/config/search/metatag/global');
285 $this->assertResponse(200);
287 'metatag_test' => 'Global description',
289 $this->drupalPostForm(NULL, $global_values, 'Save');
290 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
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);
297 'metatag_test' => 'Entity description',
299 $this->drupalPostForm(NULL, $entity_values, 'Save');
300 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
303 // Load the entity form for this entity type.
305 $this->drupalGet($this->entity_add_path);
306 $this->assertResponse(200);
307 $this->assertNoText('Fatal error');
309 // Allow the fields to be customized if needed.
310 $edit = $this->entity_default_values();
313 $this->entity_title_field . '[0][value]' => $title,
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);
325 // @todo Confirm the values output correctly.
327 // Add a field to the entity type.
330 // Open the 'edit' form for the entity.
331 $this->drupalGet($entity->toUrl('edit-form'));
332 $this->assertResponse(200);
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']);
341 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
344 // @todo Confirm the values output correctly.
348 * Confirm that the default values load correctly for an entity created after
349 * the custom field is added.
351 public function testEntityFieldValuesNewEntity() {
352 // Set a global default.
353 $this->drupalGet('admin/config/search/metatag/global');
354 $this->assertResponse(200);
356 'metatag_test' => 'Global description',
358 $this->drupalPostForm(NULL, $global_values, 'Save');
359 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
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);
366 'metatag_test' => 'Entity description',
368 $this->drupalPostForm(NULL, $entity_values, 'Save');
369 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
372 // Add a field to the entity type.
375 // Load the entity form for this entity type.
377 $this->drupalGet($this->entity_add_path);
378 $this->assertResponse(200);
379 $this->assertNoText('Fatal error');
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']);
388 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
391 // Allow the fields to be customized if needed.
392 $edit = $this->entity_default_values();
395 $this->entity_title_field . '[0][value]' => $title,
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);
407 // @todo Confirm the values output correctly.
409 // Open the 'edit' form for the entity.
410 $this->drupalGet($entity->toUrl('edit-form'));
411 $this->assertResponse(200);
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']);
420 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
423 // @todo Confirm the values output correctly.
427 * Tests adding and editing values on a given entity type.
429 public function tofix_testEntityField() {
430 // Add a field to the entity type.
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',
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);
447 // Make sure tags that have a field value but no default value still show
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.');
455 // @TODO: This should not be required, but metatags does not invalidate
456 // cache upon setting globals.
457 Cache::invalidateTags(['entity_test:' . $entity->id()]);
459 // Update the Global defaults and test them.
460 $this->drupalGet('admin/config/search/metatag/global');
461 $this->assertResponse(200);
463 'metatag_test' => 'Purple monkey dishwasher',
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.');