commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / uif / uif.test
1 <?php
2
3 /**
4 * @file
5 * UIF test code.
6 */
7
8 /**
9 * Simple import testing.
10 */
11 class UIFTestCaseSimple extends DrupalWebTestCase {
12 protected $admin_user;
13 protected $import_user;
14
15 public static function getInfo() {
16 return array(
17 'name' => 'Simple import',
18 'description' => 'Test UIF basic import.',
19 'group' => 'User Import Framework',
20 );
21 }
22
23 public function setUp() {
24 parent::setUp('uif');
25 $this->import_user = $this->drupalCreateUser(array('import users'));
26 $this->admin_user = $this->drupalCreateUser(array('administer users'));
27 }
28
29 // Do successful import
30 public function testUIFImportSuccessful() {
31 $this->drupalLogin($this->import_user);
32
33 $edit = array();
34 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_1.csv';
35 $this->drupalPost('admin/people/uif', $edit, t('Next'));
36 $this->assertText(t('Preview these records and when ready to import click Import users', array()));
37 $this->assertText(t('jane@example.com', array()));
38 $this->assertText(t('(create)', array()));
39 $this->assertText(t('mail', array()));
40 $this->assertText(t('name', array()));
41 $this->assertText(t('pass', array()));
42 $this->assertText('mail substituted for deprecated label email');
43 $this->assertText('name substituted for deprecated label username');
44 $this->assertText('pass substituted for deprecated label password');
45
46 $this->drupalPost(NULL, array(), t('Import users'));
47 $this->assertText(t('User import complete', array()));
48 $this->assertText(t('One user was created', array()));
49
50 // Retrieve imported user
51 $name = 'jane';
52 $account = user_load_by_name($name);
53 $this->assertTrue($account != NULL, t('User @name was loaded', array('@name' => $name)));
54 $account->pass_raw = 'zebra';
55 $this->drupalLogin($account);
56 }
57
58 // Do unsuccessful import
59 public function testUIFImportUnsuccessful() {
60 $this->drupalLogin($this->import_user);
61
62 $edit = array();
63 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_2_fails_no_email.csv';
64 $this->drupalPost('admin/people/uif', $edit, t('Next'));
65 $this->assertText(t('Missing or invalid email address', array()));
66 }
67 }
68
69 /**
70 * Test import with core user fields.
71 */
72 class UIFTestCaseCore extends DrupalWebTestCase {
73 protected $admin_user;
74 protected $import_user;
75
76 public static function getInfo() {
77 return array(
78 'name' => 'Import {user} table fields',
79 'description' => 'Test UIF with core {user} table fields.',
80 'group' => 'User Import Framework',
81 );
82 }
83
84 public function setUp() {
85 parent::setUp('uif', 'locale');
86 $this->import_user = $this->drupalCreateUser(array('import users'));
87 $this->admin_user = $this->drupalCreateUser(array('administer users', 'administer site configuration', 'administer languages'));
88 }
89
90 public function testUIFImportCore() {
91 $this->drupalLogin($this->import_user);
92
93 $edit = array();
94 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_core_fields_1.csv';
95 $this->drupalPost('admin/people/uif', $edit, t('Next'));
96 $this->assertText(t('Preview these records and when ready to import click Import users', array()));
97 $this->assertText(t('fred@example.com', array()));
98 $this->assertText(t('(create)', array()));
99 $this->assertText(t('mail', array()));
100 $this->assertText(t('name', array()));
101 $this->assertText(t('pass', array()));
102 $this->assertText(t('created', array()));
103 $this->assertText(t('access', array()));
104 $this->assertText(t('login', array()));
105 $this->assertText(t('status', array()));
106 $this->assertText(t('timezone', array()));
107 $this->assertText(t('language', array()));
108 $this->drupalPost(NULL, array(), t('Import users'));
109 $this->assertText(t('User import complete', array()));
110
111 $this->drupalLogin($this->admin_user);
112 $edit = array('langcode' => 'es');
113 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
114 $edit = array('configurable_timezones' => 1);
115 $this->drupalPost('admin/config/regional/settings', $edit, t('Save configuration'));
116
117 // Retrieve imported user
118 $name = 'freddie';
119 $account = user_load_by_name($name);
120 $this->assertTrue($account != NULL, t('User @name was loaded', array('@name' => $name)));
121 $account->pass_raw = '123';
122 $this->drupalLogin($account);
123
124 $this->drupalGet('user/' . $account->uid . '/edit');
125 $this->assertRaw('value="es" checked="checked"');
126 $this->assertRaw('value="Europe/Madrid" selected="selected"');
127 $this->assertRaw('name="mail" value="fred@example.com"');
128 }
129 }
130
131 /**
132 * Test import with Field module fields.
133 */
134 class UIFTestCaseFieldFields extends DrupalWebTestCase {
135 protected $admin_user;
136 protected $import_user;
137
138 public static function getInfo() {
139 return array(
140 'name' => 'Import Field module fields',
141 'description' => 'Test UIF with core Field module dependent fields.',
142 'group' => 'User Import Framework',
143 );
144 }
145
146 public function setUp() {
147 parent::setUp('uif', 'list');
148 $this->import_user = $this->drupalCreateUser(array('import users'));
149 $this->admin_user = $this->drupalCreateUser(array('administer users', 'administer site configuration', 'administer taxonomy'));
150
151 $vocabs = taxonomy_vocabulary_get_names();
152 $term = $this->createTerm($vocabs['tags'], 'granite');
153 }
154
155 /**
156 * Test addition of all Field module fields.
157 */
158 public function testUIFImportWithFieldAllFields() {
159 $fields = array(
160 array(
161 'label' => 'Boolean Test',
162 'field_name' => 'boolean_test',
163 'type' => 'list_boolean',
164 'widget_type' => 'options_buttons',
165 'check_raw' => 'Boolean Test:&nbsp;</div><div class="field-items"><div class="field-item even">1</div>',
166 ),
167 array(
168 'label' => 'Decimal Test',
169 'field_name' => 'decimal_test',
170 'type' => 'number_decimal',
171 'widget_type' => 'number',
172 'check_text' => '3.14',
173 ),
174 array(
175 'label' => 'Float Test',
176 'field_name' => 'float_test',
177 'type' => 'number_float',
178 'widget_type' => 'number',
179 'check_text' => '1.23',
180 ),
181 array(
182 'label' => 'Integer Test',
183 'field_name' => 'integer_test',
184 'type' => 'number_integer',
185 'widget_type' => 'number',
186 'check_text' => '4 844 632',
187 ),
188 array(
189 'label' => 'Float List Test',
190 'field_name' => 'float_list_test',
191 'type' => 'list_float',
192 'widget_type' => 'options_select',
193 'check_text' => '4.56',
194 ),
195 array(
196 'label' => 'Integer List Test',
197 'field_name' => 'integer_list_test',
198 'type' => 'list_integer',
199 'widget_type' => 'options_select',
200 'check_text' => '5678',
201 ),
202 array(
203 'label' => 'Test List Test',
204 'field_name' => 'text_list_test',
205 'type' => 'list_text',
206 'widget_type' => 'options_select',
207 'check_text' => 'repeat',
208 ),
209 array(
210 'label' => 'Long Text Test',
211 'field_name' => 'long_text_test',
212 'type' => 'text_long',
213 'widget_type' => 'text_textarea',
214 'check_text' => 'now is the time for',
215 ),
216 array(
217 'label' => 'Long Text and Summary Test',
218 'field_name' => 'long_text_summary_test',
219 'type' => 'text_with_summary',
220 'widget_type' => 'text_textarea_with_summary',
221 'check_text' => 'all good people to',
222 ),
223 array(
224 'label' => 'Text Test',
225 'field_name' => 'text_test',
226 'type' => 'text',
227 'widget_type' => 'text_textfield',
228 'check_text' => 'come to the aid',
229 ),
230 array(
231 'label' => 'Term Reference Test',
232 'field_name' => 'term_ref_test',
233 'type' => 'taxonomy_term_reference',
234 'widget_type' => 'options_select',
235 'check_text' => 'granite',
236 ),
237 );
238
239 $this->drupalLogin($this->admin_user);
240 foreach ($fields as $field) {
241 $edit['fields[_add_new_field][label]'] = $field['label'];
242 $edit['fields[_add_new_field][field_name]'] = $field['field_name'];
243 $edit['fields[_add_new_field][type]'] = $field['type'];
244 $edit['fields[_add_new_field][widget_type]'] = $field['widget_type'];
245 $this->drupalPost('admin/config/people/accounts/fields', $edit, t('Save'));
246 $this->assertText(t('These settings apply to the @label field everywhere it is used.', array('@label' => $field['label'])));
247
248 if ($field['type'] == 'taxonomy_term_reference') {
249 // otherwise no vocab assigned
250 $this->drupalPost(NULL, array(), t('Save field settings'));
251 }
252 }
253
254 $this->drupalLogin($this->import_user);
255 $edit = array();
256 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_field_fields_1.csv';
257 $this->drupalPost('admin/people/uif', $edit, t('Next'));
258 $this->assertText(t('Preview these records and when ready to import click Import users', array()));
259 $this->assertText('wilma@example.com');
260 $this->assertText(t('(create)', array()));
261 $this->drupalPost(NULL, array(), t('Import users'));
262 $this->assertText(t('User import complete', array()));
263
264 // Retrieve imported user
265 field_cache_clear();
266 $name = 'wilma';
267 $account = user_load_by_name($name);
268 $this->assertTrue($account != NULL, t('User @name was loaded', array('@name' => $name)));
269 $account->pass_raw = 'rubble';
270 $this->drupalLogin($account);
271
272 foreach ($fields as $field) {
273 $this->assertText($field['label']);
274 if (isset($field['check_text'])) {
275 $this->assertText($field['check_text']);
276 }
277 elseif (isset($field['check_raw'])) {
278 $this->assertRaw($field['check_raw']);
279 }
280 }
281 }
282
283 /**
284 * Test addition of one field.
285 */
286 public function testUIFImportWithFieldOneField() {
287 $this->drupalLogin($this->admin_user);
288 $edit = array();
289 $edit['fields[_add_new_field][label]'] = 'First name';
290 $edit['fields[_add_new_field][field_name]'] = 'first_name';
291 $edit['fields[_add_new_field][type]'] = 'text';
292 $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
293 $this->drupalPost('admin/config/people/accounts/fields', $edit, t('Save'));
294 $this->assertText(t('These settings apply to the @label field everywhere it is used.', array('@label' => $edit['fields[_add_new_field][label]'])));
295 $this->drupalGet('admin/config/people/accounts/display');
296 $this->assertText('No field is hidden');
297
298 $this->drupalLogin($this->import_user);
299 $edit = array();
300 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_fields_1.csv';
301 $this->drupalPost('admin/people/uif', $edit, t('Next'));
302 $this->assertText(t('Preview these records and when ready to import click Import users', array()));
303 $this->assertText(t('wilma@example.com', array()));
304 $this->assertText(t('(create)', array()));
305 $this->assertText(t('mail', array()));
306 $this->assertText(t('name', array()));
307 $this->assertText(t('pass', array()));
308 $this->assertText(t('first_name', array()));
309 $this->assertText(t('last_name', array()));
310 $this->drupalPost(NULL, array(), t('Import users'));
311 $this->assertText(t('User import complete', array()));
312 $this->assertNoText(t('Unknown column', array()));
313
314 // Retrieve imported user
315 field_cache_clear();
316 $name = 'wilma';
317 $account = user_load_by_name($name);
318 $this->assertTrue($account != NULL, t('User @name was loaded', array('@name' => $name)));
319 $account->pass_raw = 'rubble';
320 $this->drupalLogin($account);
321 $this->assertText('First name');
322 $this->assertText('Wilma');
323 }
324
325 /**
326 * Test unknown field.
327 */
328 public function testUIFImportWithFieldFieldsUnsuccessful() {
329 $this->drupalLogin($this->admin_user);
330
331 $edit = array();
332 $edit['fields[_add_new_field][label]'] = 'First name';
333 $edit['fields[_add_new_field][field_name]'] = 'first_name';
334 $edit['fields[_add_new_field][type]'] = 'text';
335 $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
336 $this->drupalPost('admin/config/people/accounts/fields', $edit, t('Save'));
337 $this->assertText(t('These settings apply to the @label field everywhere it is used.', array('@label' => $edit['fields[_add_new_field][label]'])));
338
339 $this->drupalLogin($this->import_user);
340
341 $edit = array();
342 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_fields_2_invalid_data.csv';
343 $this->drupalPost('admin/people/uif', $edit, t('Next'));
344 $this->assertText(t('Unknown column last_name in the import file', array()));
345 }
346
347 /**
348 * Returns a new vocabulary of some name.
349 */
350 private function createVocabulary($name) {
351 // Create a vocabulary.
352 $vocabulary = new stdClass();
353 $vocabulary->name = $name;
354 $vocabulary->description = $name;
355 $vocabulary->machine_name = drupal_strtolower($name);
356 $vocabulary->help = '';
357 $vocabulary->nodes = array('article' => 'article');
358 $vocabulary->weight = mt_rand(0, 10);
359 taxonomy_vocabulary_save($vocabulary);
360 return $vocabulary;
361 }
362
363 /**
364 * Returns a new term with some name.
365 */
366 private function createTerm($vocabulary, $name) {
367 $term = new stdClass();
368 $term->name = $name;
369 $term->description = $name;
370 // Use the first available text format.
371 $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
372 $term->vid = $vocabulary->vid;
373 taxonomy_term_save($term);
374 return $term;
375 }
376
377 }
378
379 /**
380 * Test import with roles.
381 */
382 class UIFTestCaseRoles extends DrupalWebTestCase {
383 protected $admin_user;
384 protected $import_user;
385
386 public static function getInfo() {
387 return array(
388 'name' => 'Import with roles',
389 'description' => 'Test UIF with role import.',
390 'group' => 'User Import Framework',
391 );
392 }
393
394 public function setUp() {
395 parent::setUp('uif');
396 $this->import_user = $this->drupalCreateUser(array('import users'));
397 $this->admin_user = $this->drupalCreateUser(array('administer users'));
398 }
399
400 /**
401 * Test role import.
402 */
403 public function testUIFImportWithRoles() {
404 $cook_rid = $this->drupalCreateRole(array(), 'cook');
405 $bottlewasher_rid = $this->drupalCreateRole(array(), 'bottlewasher');
406
407 $this->drupalLogin($this->import_user);
408
409 $edit = array();
410 $edit['files[user_upload]'] = DRUPAL_ROOT . '/' . drupal_get_path('module', 'uif') . '/test_data/test_3_roles.csv';
411 $this->drupalPost('admin/people/uif', $edit, t('Next'));
412 $this->assertText(t('Preview these records and when ready to import click Import users', array()));
413 $this->assertText(t('frodo@example.com', array()));
414 $this->assertText(t('(create)', array()));
415 $this->assertText(t('email', array()));
416 $this->assertText(t('roles', array()));
417
418 $this->drupalPost(NULL, array(), t('Import users'));
419 $this->assertText(t('User import complete', array()));
420
421 // Retrieve imported user
422 $mail = 'frodo@example.com';
423 $account = user_load_by_mail($mail);
424 $this->userLoadAndCheckRoleAssigned($account, $cook_rid);
425 $this->userLoadAndCheckRoleAssigned($account, $bottlewasher_rid, FALSE);
426
427 $mail = 'bilbo@example.com';
428 $account = user_load_by_mail($mail);
429 $this->userLoadAndCheckRoleAssigned($account, $cook_rid);
430 $this->userLoadAndCheckRoleAssigned($account, $bottlewasher_rid);
431
432 $mail = 'gandalf@example.com';
433 $account = user_load_by_mail($mail);
434 $this->userLoadAndCheckRoleAssigned($account, $cook_rid, FALSE);
435 $this->userLoadAndCheckRoleAssigned($account, $bottlewasher_rid, FALSE);
436 }
437
438 /**
439 * Check role on user object.
440 *
441 * @param object $account
442 * The user account to check.
443 * @param string $rid
444 * The role ID to search for.
445 * @param bool $is_assigned
446 * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE).
447 * Defaults to TRUE.
448 */
449 private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
450 $account = user_load($account->uid, TRUE);
451 if ($is_assigned) {
452 $this->assertTrue(array_key_exists($rid, $account->roles), t('The role is present in the user object.'));
453 }
454 else {
455 $this->assertFalse(array_key_exists($rid, $account->roles), t('The role is not present in the user object.'));
456 }
457 }
458 }