link.crud_browser.test 9.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

/**
 * @file
 * Testing CRUD API in the browser.
 */

/**
 * Testing that users can not input bad URLs or labels
 */
class LinkUITest extends DrupalWebTestcase {

  /**
   * Link supposed to be good
15
   */
16
17
18
19
  const LINK_INPUT_TYPE_GOOD = 0;

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

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

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

  function setUp() {
    parent::setUp('field_ui', 'link');
  }

  /**
   * Creates a link field for the "page" type and creates a page with a link.
   */
  function testLinkCreate() {
    //libxml_use_internal_errors(true);
45
46
47
48
49
50
51
52
53
    $this->web_user = $this->drupalCreateUser(array(
      'administer content types',
      'administer nodes',
      'administer filters',
      'access content',
      'create page content',
      'access administration pages'
    ));
    $this->drupalLogin($this->web_user);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

    // create field
    $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'));

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

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    $permission = 'create page content';
    $this->checkPermissions(array($permission), TRUE);

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

    $input_test_cases = array(
      array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName(),
        'msg' => 'Link found',
        'type' => self::LINK_INPUT_TYPE_GOOD
      ),
      array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName() . '<script>alert("hi");</script>',
        'msg' => 'js label',
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
      ),
      array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
        'msg' => 'js label',
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
      ),
      array(
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
        'msg' => 'js label',
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
      ),
107
      array(
108
109
110
111
112
        'href' => 'http://example.com/' . $this->randomName(),
        'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
        'msg' => 'js label',
        'type' => self::LINK_INPUT_TYPE_BAD_TITLE
      ),
113
      array(
114
115
116
117
118
        'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
        'label' => $this->randomName(),
        'msg' => 'js url',
        'type' => self::LINK_INPUT_TYPE_BAD_URL
      ),
119
120
121
122
123
124
      array(
        'href' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
        'label' => 'http://ecs-es.kelkoo.es/ctl/go/sitesearchGo?.ts=1338833010331&.sig=qP9GXeEFH6syBzwmzYkxmsvp1EI-',
        'msg' => 'Url with . in querystring',
        'type' => self::LINK_INPUT_TYPE_GOOD,
      ),
125
    );
126
127
128
129
130
131
132
133
    $test_case = array(
      'href' => 'www.example.com/'. $this->randomName(),
      'label' => $this->randomName(),
      'msg' => 'Link found',
      'type' => self::LINK_INPUT_TYPE_GOOD,
    );
    $test_case['expected_href'] = 'http://'. $test_case['href'];
    $input_test_cases[] = $test_case;
134

135
    foreach ($input_test_cases as $input) {
136
      $this->drupalLogin($this->web_user);
137
138
139
140
141
142
143
144
145
      $this->drupalGet('node/add/page');

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

      // change to anonym user
      $this->drupalLogout();
158

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

178
179
180
181
182
  /**
   * Testing that if you use <strong> in a static title for your link, that the
   * title actually displays <strong>.
   */
  function testStaticLinkCreate() {
183
184
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
    $this->drupalLogin($this->web_user);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

    // create field
    $name = strtolower($this->randomName());
    $field_name = 'field_'. $name;
    $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][title]' => 'value',
      'instance[settings][title_value]' => '<strong>'. $name .'</strong>'), t('Save settings'));

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

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

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

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

218
219
220
    $url = $this->getUrl();

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

224
    $this->assertRaw(l('<strong>'. $name .'</strong>', $input['href'], array('html' => TRUE)));
225
  }
226

227
228
229
230
231
  /**
   * If we're creating a new field and just hit 'save' on the default options, we want to make
   * sure they are set to the expected results.
   */
  function testCRUDCreateFieldDefaults() {
232
233
    $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
    $this->drupalLogin($this->web_user);
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

    // create field
    $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'));

    // Is field created?
    $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
    node_types_rebuild();
    menu_rebuild();
    //_content_type_info(TRUE);
    //$fields = content_fields();
    //$field = $fields['field_'. $name];
    //$field = field_info_field('field_'. $name);
    _field_info_collate_fields(TRUE);
    $instances = field_info_instances('node', 'page');
    //$this->debug($instances);
    //$this->assert('debug', '<pre>'. print_r($instances, TRUE) .'</pre>', 'Debug');
    $instance = $instances['field_'. $name];
    //$this->assertTrue(1 === $instance['validate_url'], 'Make sure validation is on.');
    $this->assertFalse($instance['required'], 'Make sure field is not required.');
    $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
    $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be off by default.');
    $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
    $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
    $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
    $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
    $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
269

270
271
272
    //$this->fail('<pre>'. print_r($fields['field_'. $name], TRUE) .'</pre>');
  }
}