3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Verify that different meta tag API options are supported.
12 class MetatagTagTypesTest extends WebTestBase {
19 protected $profile = 'testing';
24 * @var \Drupal\Core\Session\AccountInterface
33 public static $modules = [
34 // Needed for token handling.
37 // Needed for the field UI testing.
40 // Needed for the basic entity testing.
43 // Needed to verify that nothing is broken for unsupported entities.
49 // Some extra custom logic for testing Metatag.
52 // Needed for testSecureTagOption().
57 * Permissions to grant admin user.
61 protected $permissions = [
62 'access administration pages',
64 'administer entity_test fields',
65 'administer entity_test content',
66 'administer meta tags',
72 protected function setUp() {
74 $this->adminUser = $this->drupalCreateUser($this->permissions);
75 $this->drupalLogin($this->adminUser);
77 // Add a metatag field to the entity type test_entity.
78 $this->drupalGet('entity_test/structure/entity_test/fields/add-field');
79 $this->assertResponse(200);
82 'field_name' => 'metatag',
83 'new_storage_type' => 'metatag',
85 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
86 $this->drupalPostForm(NULL, [], t('Save field settings'));
87 $this->container->get('entity.manager')->clearCachedFieldDefinitions();
91 * Tests whether HTML is correctly removed from metatags.
93 * Tests three values in meta tags -- one without any HTML; one with raw html;
94 * and one with escaped HTML. To pass all HTML including escaped should be
97 public function testHtmlIsRemoved() {
98 $this->drupalGet('admin/config/search/metatag/global');
99 $this->assertResponse(200);
101 'abstract' => 'No HTML here',
102 'description' => '<html><body><p class="test">Surrounded by raw HTML</p></body></html>',
103 'keywords' => '<html><body><p class="test">Surrounded by escaped HTML</p></body></html>',
106 $this->drupalPostForm(NULL, $values, 'Save');
107 $this->assertText('Saved the Global Metatag defaults.');
108 drupal_flush_all_caches();
109 $this->drupalGet('hit-a-404');
110 $this->assertResponse(404);
112 $this->assertRaw('<meta name="abstract" content="No HTML here" />', t('Test with no HTML content'));
113 $this->assertRaw('<meta name="description" content="Surrounded by raw HTML" />', t('Test with raw HTML content'));
114 $this->assertRaw('<meta name="keywords" content="Surrounded by escaped HTML" />', t('Test with escaped HTML content'));
118 * Tests the 'secure' meta tag attribute.
120 * Tests insecure values in og:image:secure_url (a tag with secure attribute
121 * set to TRUE) and in og:image (a tag with secure attribute set to FALSE). To
122 * To pass og:image_secure should be changed to https:// and og:image
125 public function testSecureTagOption() {
126 $this->drupalGet('admin/config/search/metatag/global');
127 $this->assertResponse(200);
129 'og_image' => 'http://blahblahblah.com/insecure.jpg',
130 'og_image_secure_url' => 'http://blahblahblah.com/secure.jpg',
132 $this->drupalPostForm(NULL, $values, 'Save');
133 $this->assertText('Saved the Global Metatag defaults.');
134 drupal_flush_all_caches();
135 $this->drupalGet('');
136 $this->assertResponse(200);
138 $this->assertRaw('<meta property="og:image" content="http://blahblahblah.com/insecure.jpg" />', t('Test og:image with regular http:// link'));
139 $this->assertRaw('<meta property="og:image:secure_url" content="https://blahblahblah.com/secure.jpg" />', t('Test og:image:secure_url updated regular http:// link to https://'));
143 * Check the contact form.
145 * @todo Move this somewhere else.
147 public function testContactForm() {
148 // Test a route where the entity for that route does not implement
149 // ContentEntityInterface.
150 $controller = \Drupal::entityTypeManager()->getStorage('contact_form');
151 $controller->create([
152 'id' => 'test_contact_form',
154 $account = $this->drupalCreateUser(['access site-wide contact form']);
155 $this->drupalLogin($account);
156 $this->drupalGet('contact/test_contact_form');
157 $this->assertResponse(200);
161 * Check URL handling.
165 public function todoTestUrl() {
166 // $save_label = (floatval(\Drupal::VERSION) <= 8.3) ? t('Save and publish') : t('Save');
167 // // Tests meta tags with URLs work.
168 // $this->drupalGet($this->entity_add_path);
169 // $this->assertResponse(200);
171 // 'name[0][value]' => 'UrlTags',
172 // 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')',
173 // 'field_metatag[0][advanced][original_source]' => 'http://example.com/foo.html',
175 // $this->drupalPostForm(NULL, $edit, $save_label);
176 // $entities = entity_load_multiple_by_properties('entity_test', [
177 // 'name' => 'UrlTags',
179 // $this->assertEqual(1, count($entities), 'Entity was saved');
180 // $entity = reset($entities);
181 // $this->drupalGet($this->entity_base_path . '/' . $entity->id());
182 // $this->assertResponse(200);
183 // $elements = $this->cssSelect("meta[name='original-source']");
184 // $this->assertTrue(count($elements) === 1, 'Found original source metatag from defaults');
185 // $this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][advanced][original_source]']);