Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagTagTypesTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Verify that different meta tag API options are supported.
9  *
10  * @group metatag
11  */
12 class MetatagTagTypesTest extends WebTestBase {
13
14   /**
15    * Profile to use.
16    *
17    * @var string
18    */
19   protected $profile = 'testing';
20
21   /**
22    * Admin user.
23    *
24    * @var \Drupal\Core\Session\AccountInterface
25    */
26   protected $adminUser;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = [
34     // Needed for token handling.
35     'token',
36
37     // Needed for the field UI testing.
38     'field_ui',
39
40     // Needed for the basic entity testing.
41     'entity_test',
42
43     // Needed to verify that nothing is broken for unsupported entities.
44     'contact',
45
46     // The base module.
47     'metatag',
48
49     // Some extra custom logic for testing Metatag.
50     'metatag_test_tag',
51
52     // Needed for testSecureTagOption().
53     'metatag_open_graph',
54   ];
55
56   /**
57    * Permissions to grant admin user.
58    *
59    * @var array
60    */
61   protected $permissions = [
62     'access administration pages',
63     'view test entity',
64     'administer entity_test fields',
65     'administer entity_test content',
66     'administer meta tags',
67   ];
68
69   /**
70    * Sets the test up.
71    */
72   protected function setUp() {
73     parent::setUp();
74     $this->adminUser = $this->drupalCreateUser($this->permissions);
75     $this->drupalLogin($this->adminUser);
76
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);
80     $edit = [
81       'label' => 'Metatag',
82       'field_name' => 'metatag',
83       'new_storage_type' => 'metatag',
84     ];
85     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
86     $this->drupalPostForm(NULL, [], t('Save field settings'));
87     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
88   }
89
90   /**
91    * Tests whether HTML is correctly removed from metatags.
92    *
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
95    * removed.
96    */
97   public function testHtmlIsRemoved() {
98     $this->drupalGet('admin/config/search/metatag/global');
99     $this->assertResponse(200);
100     $values = [
101       'abstract' => 'No HTML here',
102       'description' => '<html><body><p class="test">Surrounded by raw HTML</p></body></html>',
103       'keywords' => '&lt;html&gt;&lt;body&gt;&lt;p class="test"&gt;Surrounded by escaped HTML&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;',
104     ];
105
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);
111
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'));
115   }
116
117   /**
118    * Tests the 'secure' meta tag attribute.
119    *
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
123    * unchanged.
124    */
125   public function testSecureTagOption() {
126     $this->drupalGet('admin/config/search/metatag/global');
127     $this->assertResponse(200);
128     $values = [
129       'og_image' => 'http://blahblahblah.com/insecure.jpg',
130       'og_image_secure_url' => 'http://blahblahblah.com/secure.jpg',
131     ];
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);
137
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://'));
140   }
141
142   /**
143    * Check the contact form.
144    *
145    * @todo Move this somewhere else.
146    */
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',
153     ])->save();
154     $account = $this->drupalCreateUser(['access site-wide contact form']);
155     $this->drupalLogin($account);
156     $this->drupalGet('contact/test_contact_form');
157     $this->assertResponse(200);
158   }
159
160   /**
161    * Check URL handling.
162    *
163    * @todo Finish.
164    */
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);
170     // $edit = [
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',
174     // ];
175     // $this->drupalPostForm(NULL, $edit, $save_label);
176     // $entities = entity_load_multiple_by_properties('entity_test', [
177     //   'name' => 'UrlTags',
178     // ]);
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]']);
186   }
187
188 }