Projet

Général

Profil

Paste
Télécharger (11,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / tests / link.crud_browser.test @ c8740e19

1
<?php
2

    
3
/**
4
 * @file
5
 * Testing CRUD API in the browser.
6
 */
7

    
8
/**
9
 * Testing that users can not input bad URLs or labels
10
 */
11
class LinkUITest extends DrupalWebTestcase {
12

    
13
  /**
14
   * Link supposed to be good
15
   */
16
  const LINK_INPUT_TYPE_GOOD = 0;
17

    
18
  /**
19
   * Link supposed to have a bad title
20
   */
21
  const LINK_INPUT_TYPE_BAD_TITLE = 1;
22

    
23
  /**
24
   * Link supposed to have a bad URL
25
   */
26
  const LINK_INPUT_TYPE_BAD_URL = 2;
27

    
28
  public static function getInfo() {
29
    return array(
30
      'name' => 'Link CRUD - browser test',
31
      'description' => 'Tests the field CRUD (create, read, update, delete) API 2.',
32
      'group' => 'Link',
33
    );
34
  }
35

    
36
  function setUp() {
37
    parent::setUp('field_ui', 'link');
38
  }
39

    
40
  /**
41
   * Creates a link field for the "page" type and creates a page with a link.
42
   */
43
  function testLinkCreate() {
44
    //libxml_use_internal_errors(true);
45
    $this->web_user = $this->drupalCreateUser(array(
46
      'administer content types',
47
      'administer nodes',
48
      'administer filters',
49
      'access content',
50
      'create page content',
51
      'access administration pages'
52
    ));
53
    $this->drupalLogin($this->web_user);
54

    
55
    // create field
56
    $name = strtolower($this->randomName());
57
    $edit = array(
58
      'fields[_add_new_field][label]' => $name,
59
      'fields[_add_new_field][field_name]' => $name,
60
      'fields[_add_new_field][type]' => 'link_field',
61
      'fields[_add_new_field][widget_type]' => 'link_field',
62
    );
63
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
64
    $this->drupalPost(NULL, array(), t('Save field settings'));
65
    $this->drupalPost(NULL, array(), t('Save settings'));
66

    
67
    // Is field created?
68
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
69
    node_types_rebuild();
70
    menu_rebuild();
71

    
72
    $permission = 'create page content';
73
    $this->checkPermissions(array($permission), TRUE);
74

    
75
    // create page form
76
    //$this->drupalGet('node/add');
77
    $this->drupalGet('node/add/page');
78
    $field_name = 'field_' . $name;
79
    $this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
80
    $this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
81

    
82
    $input_test_cases = array(
83
      array(
84
        'href' => 'http://example.com/' . $this->randomName(),
85
        'label' => $this->randomName(),
86
        'msg' => 'Link found',
87
        'type' => self::LINK_INPUT_TYPE_GOOD
88
      ),
89
      array(
90
        'href' => 'http://example.com/' . $this->randomName(),
91
        'label' => $this->randomName() . '<script>alert("hi");</script>',
92
        'msg' => 'js label',
93
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
94
      ),
95
      array(
96
        'href' => 'http://example.com/' . $this->randomName(),
97
        'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
98
        'msg' => 'js label',
99
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
100
      ),
101
      array(
102
        'href' => 'http://example.com/' . $this->randomName(),
103
        'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
104
        'msg' => 'js label',
105
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
106
      ),
107
      array(
108
        'href' => 'http://example.com/' . $this->randomName(),
109
        'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
110
        'msg' => 'js label',
111
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
112
      ),
113
      array(
114
        'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
115
        'label' => $this->randomName(),
116
        'msg' => 'js url',
117
        'type' => self::LINK_INPUT_TYPE_BAD_URL
118
      ),
119
      array(
120
        'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
121
        'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
122
        'msg' => 'Url with . in querystring',
123
        'type' => self::LINK_INPUT_TYPE_GOOD,
124
      ),
125
    );
126
    $test_case = array(
127
      'href' => 'www.example.com/' . $this->randomName(),
128
      'label' => $this->randomName(),
129
      'msg' => 'Link found',
130
      'type' => self::LINK_INPUT_TYPE_GOOD,
131
    );
132
    $test_case['expected_href'] = 'http://' . $test_case['href'];
133
    $input_test_cases[] = $test_case;
134

    
135
    foreach ($input_test_cases as $input) {
136
      $this->drupalLogin($this->web_user);
137
      $this->drupalGet('node/add/page');
138

    
139
      $edit = array(
140
        'title' => $input['label'],
141
        $field_name . '[und][0][title]' => $input['label'],
142
        $field_name . '[und][0][url]' => $input['href'],
143
      );
144
      $this->drupalPost(NULL, $edit, t('Save'));
145
      if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
146
        $this->assertRaw(t('The value %value provided for %field is not a valid URL.', array('%field' => $name, '%value' => trim($input['href']))), 'Not a valid URL: ' . $input['href']);
147
        continue;
148
      }
149
      else {
150
        $this->assertRaw(' ' . t('has been created.',
151
                           array('@type' => 'Basic Page', '%title' => $edit['title'])),
152
                         'Page created: ' . $input['href']);
153
      }
154
      $url = $this->getUrl();
155

    
156
      // change to Anonymous user.
157
      $this->drupalLogout();
158

    
159
      $this->drupalGet($url);
160
      //debug($this);
161
      // If simpletest starts using something to override the error system, this will flag
162
      // us and let us know it's broken.
163
      $this->assertFalse(libxml_use_internal_errors(TRUE));
164
      if (isset($input['expected_href'])) {
165
        $path = '//a[@href="' . $input['expected_href'] . '" and text()="' . $input['label'] . '"]';
166
      }
167
      else {
168
        $path = '//a[@href="' . $input['href'] . '" and text()="' . $input['label'] . '"]';
169
      }
170
      $elements = $this->xpath($path);
171
      libxml_use_internal_errors(FALSE);
172
      $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
173
    }
174
    //libxml_use_internal_errors(FALSE);
175
  }
176

    
177
  /**
178
   * Testing that if you use <strong> in a static title for your link, that the
179
   * title actually displays <strong>.
180
   */
181
  function testStaticLinkCreate() {
182
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
183
    $this->drupalLogin($this->web_user);
184

    
185
    // create field
186
    $name = strtolower($this->randomName());
187
    $field_name = 'field_' . $name;
188
    $edit = array(
189
      'fields[_add_new_field][label]' => $name,
190
      'fields[_add_new_field][field_name]' => $name,
191
      'fields[_add_new_field][type]' => 'link_field',
192
      'fields[_add_new_field][widget_type]' => 'link_field',
193
    );
194
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
195
    $this->drupalPost(NULL, array(), t('Save field settings'));
196
    $this->drupalPost(NULL, array(
197
      'instance[settings][title]' => 'value',
198
      'instance[settings][title_value]' => '<strong>' . $name . '</strong>'), t('Save settings'));
199

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

    
203
    // create page form
204
    $this->drupalGet('node/add/page');
205
    $this->assertField($field_name . '[und][0][url]', 'URL found');
206

    
207
    $input = array(
208
      'href' => 'http://example.com/' . $this->randomName()
209
    );
210

    
211
    $edit = array(
212
      'title' => $name,
213
      $field_name . '[und][0][url]' => $input['href'],
214
    );
215
    $this->drupalPost(NULL, $edit, t('Save'));
216

    
217
    $url = $this->getUrl();
218

    
219
    // change to anonymous user
220
    $this->drupalLogout();
221
    $this->drupalGet($url);
222

    
223
    $this->assertRaw(l('<strong>' . $name . '</strong>', $input['href'], array('html' => TRUE)));
224
  }
225
  
226
  /**
227
   * Testing that if you have the title but no url, the title is not sanitized twice.
228
   */
229
  function testCRUDTitleOnlyTitleNoLink() {
230
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
231
    $this->drupalLogin($this->web_user);
232

    
233
    // create field
234
    $name = strtolower($this->randomName());
235
    $field_name = 'field_' . $name;
236
    $edit = array(
237
      'fields[_add_new_field][label]' => $name,
238
      'fields[_add_new_field][field_name]' => $name,
239
      'fields[_add_new_field][type]' => 'link_field',
240
      'fields[_add_new_field][widget_type]' => 'link_field',
241
    );
242
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
243
    $this->drupalPost(NULL, array(), t('Save field settings'));
244
    $this->drupalPost(NULL, array(
245
      'instance[settings][url]' => 1,
246
    ), t('Save settings'));
247

    
248
    // Is field created?
249
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
250
    
251
    // create page form
252
    $this->drupalGet('node/add/page');
253
    $this->assertField($field_name . '[und][0][url]', 'URL found');
254

    
255
    $input = array(
256
      'title' => 'This & That',
257
      'href' => '',
258
    );
259

    
260
    $edit = array(
261
      'title' => $name,
262
      $field_name . '[und][0][title]' => $input['title'],
263
      $field_name . '[und][0][url]' => $input['href'],
264
    );
265
    $this->drupalPost(NULL, $edit, t('Save'));
266

    
267
    $url = $this->getUrl();
268
    
269
    // change to anonymous user
270
    $this->drupalLogout();
271
    $this->drupalGet($url);
272

    
273
    $this->assertRaw('This &amp; That');
274
  }
275

    
276
  /**
277
   * If we're creating a new field and just hit 'save' on the default options, we want to make
278
   * sure they are set to the expected results.
279
   */
280
  function testCRUDCreateFieldDefaults() {
281
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
282
    $this->drupalLogin($this->web_user);
283

    
284
    // create field
285
    $name = strtolower($this->randomName());
286
    $edit = array(
287
      'fields[_add_new_field][label]' => $name,
288
      'fields[_add_new_field][field_name]' => $name,
289
      'fields[_add_new_field][type]' => 'link_field',
290
      'fields[_add_new_field][widget_type]' => 'link_field',
291
    );
292
    $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
293
    $this->drupalPost(NULL, array(), t('Save field settings'));
294
    $this->drupalPost(NULL, array(), t('Save settings'));
295

    
296
    // Is field created?
297
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
298
    node_types_rebuild();
299
    menu_rebuild();
300

    
301
    _field_info_collate_fields(TRUE);
302
    $instances = field_info_instances('node', 'page');
303

    
304
    $instance = $instances['field_' . $name];
305
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
306
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
307
    $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
308
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
309
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
310
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
311
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
312
    $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
313
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
314
  }
315
}