Mercurial > hg > drupalISMI
comparison openmindattribute/tests/link.crud_browser.test @ 0:124ef8f3b22d
initial
author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
---|---|
date | Fri, 27 Mar 2015 19:21:42 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:124ef8f3b22d |
---|---|
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 provided for %field is not a valid URL.', array('%field' => $name)), '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 anonym 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 //$this->pass(htmlentities($path)); | |
171 $elements = $this->xpath($path); | |
172 libxml_use_internal_errors(FALSE); | |
173 $this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']); | |
174 } | |
175 //libxml_use_internal_errors(FALSE); | |
176 } | |
177 | |
178 /** | |
179 * Testing that if you use <strong> in a static title for your link, that the | |
180 * title actually displays <strong>. | |
181 */ | |
182 function testStaticLinkCreate() { | |
183 $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content')); | |
184 $this->drupalLogin($this->web_user); | |
185 | |
186 // create field | |
187 $name = strtolower($this->randomName()); | |
188 $field_name = 'field_'. $name; | |
189 $edit = array( | |
190 'fields[_add_new_field][label]' => $name, | |
191 'fields[_add_new_field][field_name]' => $name, | |
192 'fields[_add_new_field][type]' => 'link_field', | |
193 'fields[_add_new_field][widget_type]' => 'link_field', | |
194 ); | |
195 $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save')); | |
196 $this->drupalPost(NULL, array(), t('Save field settings')); | |
197 $this->drupalPost(NULL, array( | |
198 'instance[settings][title]' => 'value', | |
199 'instance[settings][title_value]' => '<strong>'. $name .'</strong>'), t('Save settings')); | |
200 | |
201 // Is field created? | |
202 $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added'); | |
203 | |
204 // create page form | |
205 $this->drupalGet('node/add/page'); | |
206 $this->assertField($field_name . '[und][0][url]', 'URL found'); | |
207 | |
208 $input = array( | |
209 'href' => 'http://example.com/' . $this->randomName() | |
210 ); | |
211 | |
212 $edit = array( | |
213 'title' => $name, | |
214 $field_name . '[und][0][url]' => $input['href'], | |
215 ); | |
216 $this->drupalPost(NULL, $edit, t('Save')); | |
217 | |
218 $url = $this->getUrl(); | |
219 | |
220 // change to anonymous user | |
221 $this->drupalLogout(); | |
222 $this->drupalGet($url); | |
223 | |
224 $this->assertRaw(l('<strong>'. $name .'</strong>', $input['href'], array('html' => TRUE))); | |
225 } | |
226 | |
227 /** | |
228 * If we're creating a new field and just hit 'save' on the default options, we want to make | |
229 * sure they are set to the expected results. | |
230 */ | |
231 function testCRUDCreateFieldDefaults() { | |
232 $this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content')); | |
233 $this->drupalLogin($this->web_user); | |
234 | |
235 // create field | |
236 $name = strtolower($this->randomName()); | |
237 $edit = array( | |
238 'fields[_add_new_field][label]' => $name, | |
239 'fields[_add_new_field][field_name]' => $name, | |
240 'fields[_add_new_field][type]' => 'link_field', | |
241 'fields[_add_new_field][widget_type]' => 'link_field', | |
242 ); | |
243 $this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save')); | |
244 $this->drupalPost(NULL, array(), t('Save field settings')); | |
245 $this->drupalPost(NULL, array(), t('Save settings')); | |
246 | |
247 // Is field created? | |
248 $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added'); | |
249 node_types_rebuild(); | |
250 menu_rebuild(); | |
251 //_content_type_info(TRUE); | |
252 //$fields = content_fields(); | |
253 //$field = $fields['field_'. $name]; | |
254 //$field = field_info_field('field_'. $name); | |
255 _field_info_collate_fields(TRUE); | |
256 $instances = field_info_instances('node', 'page'); | |
257 //$this->debug($instances); | |
258 //$this->assert('debug', '<pre>'. print_r($instances, TRUE) .'</pre>', 'Debug'); | |
259 $instance = $instances['field_'. $name]; | |
260 //$this->assertTrue(1 === $instance['validate_url'], 'Make sure validation is on.'); | |
261 $this->assertFalse($instance['required'], 'Make sure field is not required.'); | |
262 $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.'); | |
263 $this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be off by default.'); | |
264 $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.'); | |
265 $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"'); | |
266 $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.'); | |
267 $this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.'); | |
268 $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.'); | |
269 | |
270 //$this->fail('<pre>'. print_r($fields['field_'. $name], TRUE) .'</pre>'); | |
271 } | |
272 } |