Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagTagTypesTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Verify that different meta tag API options are supported.
10  *
11  * @group metatag
12  */
13 class MetatagTagTypesTest extends WebTestBase {
14
15   /**
16    * Profile to use.
17    */
18   protected $profile = 'testing';
19
20   /**
21    * Admin user
22    *
23    * @var \Drupal\Core\Session\AccountInterface
24    */
25   protected $adminUser;
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = [
33     // Needed for token handling.
34     'token',
35
36     // Needed for the field UI testing.
37     'field_ui',
38
39     // Needed for the basic entity testing.
40     'entity_test',
41
42     // Needed to verify that nothing is broken for unsupported entities.
43     'contact',
44
45     // The base module.
46     'metatag',
47
48     // Some extra custom logic for testing Metatag.
49     'metatag_test_tag',
50
51     // Needed for testSecureTagOption().
52     'metatag_open_graph',
53   ];
54
55   /**
56    * Permissions to grant admin user.
57    *
58    * @var array
59    */
60   protected $permissions = [
61     'access administration pages',
62     'view test entity',
63     'administer entity_test fields',
64     'administer entity_test content',
65     'administer meta tags',
66   ];
67
68   /**
69    * Sets the test up.
70    */
71   protected function setUp() {
72     parent::setUp();
73     $this->adminUser = $this->drupalCreateUser($this->permissions);
74     $this->drupalLogin($this->adminUser);
75
76     // Add a metatag field to the entity type test_entity.
77     $this->drupalGet('entity_test/structure/entity_test/fields/add-field');
78     $this->assertResponse(200);
79     $edit = [
80       'label' => 'Metatag',
81       'field_name' => 'metatag',
82       'new_storage_type' => 'metatag',
83     ];
84     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
85     $this->drupalPostForm(NULL, [], t('Save field settings'));
86     $this->container->get('entity.manager')->clearCachedFieldDefinitions();
87   }
88
89   /**
90    * Tests whether HTML is correctly removed from metatags
91    *
92    * Tests three values in metatags -- one without any HTML; one with raw html; and one with escaped HTML.
93    * To pass all HTML including escaped should be removed.
94    */
95   public function testHTMLIsRemoved() {
96     $this->drupalGet('admin/config/search/metatag/global');
97     $this->assertResponse(200);
98     $values = [
99       'abstract' => 'No HTML here',
100       'description' => '<html><body><p class="test">Surrounded by raw HTML</p></body></html>',
101       '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;',
102     ];
103
104     $this->drupalPostForm(NULL, $values, 'Save');
105     $this->assertText('Saved the Global Metatag defaults.');
106     drupal_flush_all_caches();
107     $this->drupalGet('hit-a-404');
108     $this->assertResponse(404);
109
110     $this->assertRaw('<meta name="abstract" content="No HTML here" />', t('Test with no HTML content'));
111     $this->assertRaw('<meta name="description" content="Surrounded by raw HTML" />', t('Test with raw HTML content'));
112     $this->assertRaw('<meta name="keywords" content="Surrounded by escaped HTML" />', t('Test with escaped HTML content'));
113   }
114
115   /**
116    * Tests whether insecure links in Tags with attribute secure = TRUE are
117    * correctly changed to secure links
118    *
119    * Tests insecure values in og:image:secure_url (a tag with secure attribue
120    * set to TRUE) and in og:image (a tag with secure attribue set to FALSE). To
121    * To pass og:image_secure should be changed to https:// and og:image
122    * unchanged.
123    */
124   public function testSecureTagOption() {
125     $this->drupalGet('admin/config/search/metatag/global');
126     $this->assertResponse(200);
127     $values = [
128       'og_image' => 'http://blahblahblah.com/insecure.jpg',
129       'og_image_secure_url' => 'http://blahblahblah.com/secure.jpg',
130     ];
131     $this->drupalPostForm(NULL, $values, 'Save');
132     $this->assertText('Saved the Global Metatag defaults.');
133     drupal_flush_all_caches();
134     $this->drupalGet('');
135     $this->assertResponse(200);
136
137     $this->assertRaw('<meta property="og:image" content="http://blahblahblah.com/insecure.jpg" />', t('Test og:image with regular http:// link'));
138     $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:// '));
139   }
140
141   /**
142    * @todo Move this somewhere else.
143    */
144   function testContactForm() {
145     // Test a route where the entity for that route does not implement
146     // ContentEntityInterface.
147     $controller = \Drupal::entityTypeManager()->getStorage('contact_form');
148     $controller->create([
149       'id' => 'test_contact_form',
150     ])->save();
151     $account = $this->drupalCreateUser(['access site-wide contact form']);
152     $this->drupalLogin($account);
153     $this->drupalGet('contact/test_contact_form');
154     $this->assertResponse(200);
155   }
156
157   /**
158    * @todo Finish.
159    */
160   // public function testUrl() {
161   //   // Tests metatags with URLs work.
162   //   $this->drupalGet($this->entity_add_path);
163   //   $this->assertResponse(200);
164   //   $edit = [
165   //     'name[0][value]' => 'UrlTags',
166   //     'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')',
167   //     'field_metatag[0][advanced][original_source]' => 'http://example.com/foo.html',
168   //   ];
169   //   $this->drupalPostForm(NULL, $edit, t('Save'));
170   //   $entities = entity_load_multiple_by_properties('entity_test', [
171   //     'name' => 'UrlTags',
172   //   ]);
173   //   $this->assertEqual(1, count($entities), 'Entity was saved');
174   //   $entity = reset($entities);
175   //   $this->drupalGet($this->entity_base_path . '/' . $entity->id());
176   //   $this->assertResponse(200);
177   //   $elements = $this->cssSelect("meta[name='original-source']");
178   //   $this->assertTrue(count($elements) === 1, 'Found original source metatag from defaults');
179   //   $this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][advanced][original_source]']);
180   // }
181
182 }