Projet

Général

Profil

Paste
Télécharger (12,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / LinkFieldValidateTest.test @ bad4e148

1
<?php
2

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

    
8
/**
9
 * Link field Validation Tests.
10
 */
11
class LinkFieldValidateTest extends LinkBaseTestClass {
12

    
13
  /**
14
   * Get Info.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Link field validation tests',
19
      'description' => 'Tests the field validation.',
20
      'group' => 'Link',
21
    );
22
  }
23

    
24
  /**
25
   * Create Link.
26
   */
27
  protected function createLink($url, $title, $attributes = array()) {
28
    return array(
29
      'url' => $url,
30
      'title' => $title,
31
      'attributes' => $attributes,
32
    );
33
  }
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
          array(
47
            'title' => $label,
48
            '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
   * Validate basic URL.
63
   */
64
  public function testLinkValidateBasicUrl() {
65
    $this->linkTestValidateUrl('http://www.example.com');
66
  }
67

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

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

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

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

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

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

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

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

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

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

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

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

    
167
  }
168

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

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

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

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

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

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

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

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

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

    
259
    $this->acquireNodes(2);
260

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

    
263
    $edit = array();
264
    $title = $this->randomName();
265
    $url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
266
    $edit[$field['field_name'] . '[0][url]'] = $url;
267
    $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
      '%field' => $name,
275
      '%value' => trim($url),
276
    )));
277

    
278
    // Make sure we get a new version!
279
    $node = node_load($this->nodes[0]->nid, NULL, TRUE);
280
    $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
    // Turn the array validation back _on_.
286
    $edit = array('validate_url' => TRUE);
287
    $node_type_link = str_replace('_', '-', $node->type);
288
    // @codingStandardsIgnoreLine
289
    // $this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
290
    // $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
    // This actually works because the display_url goes through the core
295
    // url() function.  But we should have a test that makes sure it continues
296
    // to work.
297
    $this->assertNoRaw($url, 'Make sure Javascript does not display.');
298
    // $this->fail($this->content);.
299
  }
300

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

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

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

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

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

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

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

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

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

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

    
370
  /**
371
   * Check German b in URL, aka Eszett.
372
   */
373
  public function testGermanEszettUrl() {
374
    $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
  public function testCurlyBracketsInQuery() {
388
    $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
   * Here, we're testing that a very long url is stored properly in the db.
393
   *
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
}