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->drupalLogin($this->adminUser);
140 * Any additional configuration that's needed for this entity type.
142 protected function setUpEntityType() {}
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.
150 protected function entity_default_values() {}
153 * Add a Metatag field to this entity type.
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);
160 'label' => 'Metatag',
161 'field_name' => 'metatag',
162 'new_storage_type' => 'metatag',
164 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
165 $this->drupalPostForm(NULL, [], t('Save field settings'));
167 // Clear all settings.
168 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
172 * Confirm that the global default values work correctly when there are no
173 * entity or bundle defaults available.
175 public function testGlobalDefaultsInheritance() {
176 // First we set global defaults.
177 $this->drupalGet('admin/config/search/metatag/global');
178 $this->assertResponse(200);
180 'metatag_test' => 'Global description',
182 $this->drupalPostForm(NULL, $global_values, 'Save');
183 $this->assertText('Saved the Global Metatag defaults.');
185 // Add the field to this entity type.
188 // Now when we create an entity, global defaults are used to fill the form
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.'));
196 * Confirm that the entity default values work correctly.
198 public function testEntityDefaultsInheritance() {
199 // This test doesn't make sense if the entity doesn't support defaults.
200 if (!$this->entity_supports_defaults) {
204 // Set a global default.
205 $this->drupalGet('admin/config/search/metatag/global');
206 $this->assertResponse(200);
208 'metatag_test' => 'Global description',
210 $this->drupalPostForm(NULL, $global_values, 'Save');
211 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
213 // Set an entity default.
214 $this->drupalGet('admin/config/search/metatag/' . $this->entity_type);
215 $this->assertResponse(200);
217 'metatag_test' => 'Entity description',
219 $this->drupalPostForm(NULL, $entity_values, 'Save');
220 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
222 // Add the field to this entity type.
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');
230 // Allow the fields to be customized if needed.
232 $edit = $this->entity_default_values();
235 $this->entity_title_field . '[0][value]' => $title,
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']);
246 * Confirm that the default values for an entity bundle will work correctly
247 * when there is no field for overriding the defaults.
250 // public function testBundleDefaultsInheritance() {
254 * Confirm a field can be added to the entity bundle.
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>');
263 * Confirm a field can be added to the entity bundle.
265 public function testEntityFieldsAvailable() {
266 // Add a field to the entity type.
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]');
277 * Confirm that the default values load correctly for an entity created before
278 * the custom field is added.
280 public function testEntityFieldValuesOldEntity() {
281 // Set a global default.
282 $this->drupalGet('admin/config/search/metatag/global');
283 $this->assertResponse(200);
285 'metatag_test' => 'Global description',
287 $this->drupalPostForm(NULL, $global_values, 'Save');
288 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
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);
295 'metatag_test' => 'Entity description',
297 $this->drupalPostForm(NULL, $entity_values, 'Save');
298 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
301 // Load the entity form for this entity type.
303 $this->drupalGet($this->entity_add_path);
304 $this->assertResponse(200);
305 $this->assertNoText('Fatal error');
307 // Allow the fields to be customized if needed.
308 $edit = $this->entity_default_values();
311 $this->entity_title_field . '[0][value]' => $title,
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,
320 $this->assertEqual(1, count($entities), 'Entity was saved');
321 $entity = reset($entities);
323 // @todo Confirm the values output correctly.
325 // Add a field to the entity type.
328 // Open the 'edit' form for the entity.
329 $this->drupalGet($entity->toUrl('edit-form'));
330 $this->assertResponse(200);
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']);
339 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
342 // @todo Confirm the values output correctly.
346 * Confirm that the default values load correctly for an entity created after
347 * the custom field is added.
349 public function testEntityFieldValuesNewEntity() {
350 // Set a global default.
351 $this->drupalGet('admin/config/search/metatag/global');
352 $this->assertResponse(200);
354 'metatag_test' => 'Global description',
356 $this->drupalPostForm(NULL, $global_values, 'Save');
357 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t('Global')])));
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);
364 'metatag_test' => 'Entity description',
366 $this->drupalPostForm(NULL, $entity_values, 'Save');
367 $this->assertText(strip_tags(t('Saved the %label Metatag defaults.', ['%label' => t($this->entity_label)])));
370 // Add a field to the entity type.
373 // Load the entity form for this entity type.
375 $this->drupalGet($this->entity_add_path);
376 $this->assertResponse(200);
377 $this->assertNoText('Fatal error');
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']);
386 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
389 // Allow the fields to be customized if needed.
390 $edit = $this->entity_default_values();
393 $this->entity_title_field . '[0][value]' => $title,
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,
402 $this->assertEqual(1, count($entities), 'Entity was saved');
403 $entity = reset($entities);
405 // @todo Confirm the values output correctly.
407 // Open the 'edit' form for the entity.
408 $this->drupalGet($entity->toUrl('edit-form'));
409 $this->assertResponse(200);
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']);
418 $this->assertFieldByName('field_metatag[0][basic][metatag_test]', $global_values['metatag_test']);
421 // @todo Confirm the values output correctly.
425 * Tests adding and editing values on a given entity type.
427 public function tofix_testEntityField() {
428 // Add a field to the entity type.
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',
438 $this->drupalPostForm(NULL, $edit, t('Save'));
439 $entities = entity_load_multiple_by_properties('entity_test', [
440 $this->entity_title_field => 'Barfoo',
442 $this->assertEqual(1, count($entities), 'Entity was saved');
443 $entity = reset($entities);
445 // Make sure tags that have a field value but no default value still show
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.');
453 // @TODO: This should not be required, but metatags does not invalidate
454 // cache upon setting globals.
455 Cache::invalidateTags(['entity_test:' . $entity->id()]);
457 // Update the Global defaults and test them.
458 $this->drupalGet('admin/config/search/metatag/global');
459 $this->assertResponse(200);
461 'metatag_test' => 'Purple monkey dishwasher',
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.');