LinkFieldValidateTest.test 12.4 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Link field Validation Tests.
6
7
 */

8
/**
9
 * Link field Validation Tests.
10
 */
11
12
13
14
15
16
17
18
19
20
21
22
class LinkFieldValidateTest extends LinkBaseTestClass {

  /**
   * Get Info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Link field validation tests',
      'description' => 'Tests the field validation.',
      'group' => 'Link',
    );
  }
23

24
25
26
  /**
   * Create Link.
   */
27
  protected function createLink($url, $title, $attributes = array()) {
28
29
30
31
32
33
    return array(
      'url' => $url,
      'title' => $title,
      'attributes' => $attributes,
    );
  }
34

35
  /**
36
   * Takes a URL, sees if it can validate that the URL is valid.
37
   */
38
  protected function linkTestValidateUrl($url) {
39
    $field_name = $this->createLinkField();
40

41
    $label = $this->randomName();
42
    $settings = array(
43
      'title' => $label,
44
      $field_name => array(
45
        LANGUAGE_NONE => array(
46
47
48
          array(
            'title' => $label,
            'url' => $url,
49
          ),
50
51
        ),
      ),
52
    );
53

54
    $node = $this->drupalCreateNode($settings);
55

56
    $this->assertNotNull($node, ' has been created.', 'Node created');
57

58
    $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
59
60
  }

61
62
63
  /**
   * Validate basic URL.
   */
64
65
  public function testLinkValidateBasicUrl() {
    $this->linkTestValidateUrl('http://www.example.com');
66
  }
67

68
69
70
  /**
   * Test if we're stopped from posting a bad url on default validation.
   */
71
  public function testLinkValidateBadUrlValidateDefault() {
72
73
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
74
75
76
77
78
79
80
      'administer fields',
      'administer nodes',
      'administer filters',
      'access content',
      'create page content',
      'access administration pages',
    ));
81
    $this->drupalLogin($this->web_user);
82

83
    // Create field.
84
85
86
87
88
89
90
91
92
93
    $name = strtolower($this->randomName());
    $edit = array(
      'fields[_add_new_field][label]' => $name,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'link_field',
      'fields[_add_new_field][widget_type]' => 'link_field',
    );
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(), t('Save field settings'));
    $this->drupalPost(NULL, array(), t('Save settings'));
94

95
96
97
98
    // Is field created?
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
    node_types_rebuild();
    menu_rebuild();
99

100
    // Create page form.
101
102
    $this->drupalGet('node/add/page');
    $field_name = 'field_' . $name;
103
104
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
105

106
107
    $edit = array(
      'title' => 'Simple Title',
108
      $field_name . '[und][0][url]' => 'edik:naw',
109
    );
110

111
    $this->drupalPost(NULL, $edit, t('Save'));
112
113
114
115
    $this->assertText(t('The value @value provided for @field is not a valid URL.', array(
      '@value' => 'edik:naw',
      '@field' => $name,
    )));
116
  }
117

118
119
120
  /**
   * Test if we're stopped from posting a bad url with validation on.
   */
121
  public function testLinkValidateBadUrlValidateOn() {
122
123
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
124
125
126
127
128
129
130
      'administer fields',
      'administer nodes',
      'administer filters',
      'access content',
      'create page content',
      'access administration pages',
    ));
131
    $this->drupalLogin($this->web_user);
132

133
    // Create field.
134
135
136
137
138
139
140
141
142
143
    $name = strtolower($this->randomName());
    $edit = array(
      'fields[_add_new_field][label]' => $name,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'link_field',
      'fields[_add_new_field][widget_type]' => 'link_field',
    );
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(), t('Save field settings'));
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
144

145
146
147
148
    // Is field created?
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
    node_types_rebuild();
    menu_rebuild();
149

150
    // Create page form.
151
152
    $this->drupalGet('node/add/page');
    $field_name = 'field_' . $name;
153
154
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
155

156
157
    $edit = array(
      'title' => 'Simple Title',
158
      $field_name . '[und][0][url]' => 'edik:naw',
159
    );
160

161
    $this->drupalPost(NULL, $edit, t('Save'));
162
163
164
165
    $this->assertText(t('The value @value provided for @field is not a valid URL.', array(
      '@field' => $name,
      '@value' => 'edik:naw',
    )));
166

167
  }
168

169
170
171
  /**
   * Test if we can post a bad url if the validation is expressly turned off.
   */
172
  public function testLinkValidateBadUrlValidateOff() {
173
174
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
175
176
177
178
179
180
181
      'administer fields',
      'administer nodes',
      'administer filters',
      'access content',
      'create page content',
      'access administration pages',
    ));
182
    $this->drupalLogin($this->web_user);
183

184
    // Create field.
185
186
187
188
189
190
191
192
193
194
    $name = strtolower($this->randomName());
    $edit = array(
      'fields[_add_new_field][label]' => $name,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'link_field',
      'fields[_add_new_field][widget_type]' => 'link_field',
    );
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
    $this->drupalPost(NULL, array(), t('Save field settings'));
    $this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
195

196
    // @codingStandardsIgnoreLine
197
198
199
    /*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
    $this->fail('<pre>'. print_r($instance_details, TRUE) .'</pre>');
    $this->fail('<pre>'. print_r(unserialize($instance_details->data), TRUE) .'</pre>');*/
200

201
202
203
204
    // Is field created?
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
    node_types_rebuild();
    menu_rebuild();
205

206
    // Create page form.
207
208
    $this->drupalGet('node/add/page');
    $field_name = 'field_' . $name;
209
210
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
211

212
213
    $edit = array(
      'title' => 'Simple Title',
214
      $field_name . '[und][0][url]' => 'edik:naw',
215
    );
216

217
    $this->drupalPost(NULL, $edit, t('Save'));
218
219
220
221
    $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
      '%field' => $name,
      '%value' => 'edik:naw',
    )));
222
  }
223

224
  /**
225
226
227
228
   * Validate switching between validation status.
   *
   * Test if a bad url can sneak through un-filtered if we play with the
   * validation...
229
230
   *
   * @todo Enable this, fix any problems that come up.
231
   */
232
  public function xTestLinkValidateSwitchingBetweenValidationStatus() {
233
    $this->acquireContentTypes(1);
234
235
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
236
237
238
239
240
241
242
      'administer fields',
      'administer nodes',
      'access administration pages',
      'access content',
      'create ' . $this->content_types[0]->type . ' content',
      'edit any ' . $this->content_types[0]->type . ' content',
    ));
243
    $this->drupalLogin($this->web_user);
244
245
246
247
    variable_set('node_options_' . $this->content_types[0]->name, array(
      'status',
      'promote',
    ));
248
249
250
251
    $field_settings = array(
      'type' => 'link',
      'widget_type' => 'link',
      'type_name' => $this->content_types[0]->name,
252
      // <-- This is needed or we have an error.
253
      'attributes' => array(),
254
255
256
257
258
259
      'validate_url' => 0,
    );

    $field = $this->createField($field_settings, 0);

    $this->acquireNodes(2);
260

261
    $this->drupalGet('node/' . $this->nodes[0]->nid);
262

263
264
265
    $edit = array();
    $title = $this->randomName();
    $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
266
267
    $edit[$field['field_name'] . '[0][url]'] = $url;
    $edit[$field['field_name'] . '[0][title]'] = $title;
268

269
    $this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
270
    // $this->pass($this->content);.
271
    // @codingStandardsIgnoreLine
272
    $this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
273
      // @codingStandardsIgnoreLine
274
275
276
      '%field' => $name,
      '%value' => trim($url),
    )));
277

278
279
280
    // Make sure we get a new version!
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
    $this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
281

282
    $this->drupalGet('node/' . $node->nid);
283
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
284

285
286
287
    // Turn the array validation back _on_.
    $edit = array('validate_url' => TRUE);
    $node_type_link = str_replace('_', '-', $node->type);
288
289
290
    // @codingStandardsIgnoreLine
    // $this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
    // $this->fail($this->content);.
291
    $this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
292

293
    $this->drupalGet('node/' . $node->nid);
294
295
296
297
    // This actually works because the display_url goes through the core
    // url() function.  But we should have a test that makes sure it continues
    // to work.
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
298
    // $this->fail($this->content);.
299
  }
300

301
302
303
  /**
   * Validate that '<front>' is a valid url.
   */
304
305
  public function testLinkFrontUrl() {
    $this->linkTestValidateUrl('<front>');
306
  }
307

308
309
310
  /**
   * Validate that an internal url would be accepted.
   */
311
  public function testLinkInternalUrl() {
312
313
314
315
    // Create the content first.
    $node = $this->drupalCreateNode();

    $link = 'node/' . $node->nid;
316
    $this->linkTestValidateUrl($link);
317
318
    $type = link_url_type($link);
    $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
319
  }
320

321
322
323
  /**
   * Validate a simple mailto.
   */
324
325
  public function testLinkMailto() {
    $this->linkTestValidateUrl('mailto:jcfiala@gmail.com');
326
327
  }

328
329
330
  /**
   * Check link external https.
   */
331
332
  public function testLinkExternalHttps() {
    $this->linkTestValidateUrl('https://www.example.com/');
333
  }
334

335
336
337
  /**
   * Check link FTP.
   */
338
339
  public function testLinkFtp() {
    $this->linkTestValidateUrl('ftp://www.example.com/');
340
341
  }

342
343
344
  /**
   * Validate a news link to a message group.
   */
345
346
  public function testLinkNews() {
    $this->linkTestValidateUrl('news:comp.infosystems.www.misc');
347
  }
348

349
350
351
  /**
   * Validate a news link to a message id.  Said ID copied off of google groups.
   */
352
353
  public function testLinkNewsMessage() {
    $this->linkTestValidateUrl('news:hj0db8$vrm$1@news.eternal-september.org');
354
355
  }

356
357
358
  /**
   * Lets throw in a lot of umlouts for testing!
   */
359
360
  public function testUmloutUrl() {
    $this->linkTestValidateUrl('http://üÜü.exämple.com/nöde');
361
  }
362

363
364
365
  /**
   * Check umlout mailto.
   */
366
367
  public function testUmloutMailto() {
    $this->linkTestValidateUrl('mailto:Üser@exÅmple.com');
368
  }
369

370
  /**
371
   * Check German b in URL, aka Eszett.
372
   */
373
374
  public function testGermanEszettUrl() {
    $this->linkTestValidateUrl('http://www.test.com/ßstuff');
375
  }
376

377
  /**
378
   * Check Spanish ñ in URL.
379
   */
380
  public function testSpecialEneUrl() {
381
    $this->linkTestValidateUrl('http://www.testÑñ.com/');
382
  }
383

384
  /**
385
   * Curly brackets in query.
386
   */
387
388
  public function testCurlyBracketsInQuery() {
    $this->linkTestValidateUrl('http://www.healthyteennetwork.org/index.asp?Type=B_PR&SEC={2AE1D600-4FC6-4B4D-8822-F1D5F072ED7B}&DE={235FD1E7-208D-4363-9854-4E6775EB8A4C}');
389
  }
390

391
392
393
  /**
   * Here, we're testing that a very long url is stored properly in the db.
   *
394
   * Basically, trying to test http://drupal.org/node/376818
395
   */
396
  public function testLinkUrlFieldIsBig() {
397
    $long_url = 'http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A';
398
    $this->linkTestValidateUrl($long_url);
399
  }
400

401
}