9 * Simple import testing.
11 class UIFTestCaseSimple extends DrupalWebTestCase {
12 protected $admin_user;
13 protected $import_user;
15 public static function getInfo() {
17 'name' => 'Simple import',
18 'description' => 'Test UIF basic import.',
19 'group' => 'User Import Framework',
23 public function setUp() {
25 $this->import_user = $this->drupalCreateUser(array('import users'));
26 $this->admin_user = $this->drupalCreateUser(array('administer users'));
29 // Do successful import
30 public function testUIFImportSuccessful() {
31 $this->drupalLogin($this->import_user);
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');
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()));
50 // Retrieve imported user
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);
58 // Do unsuccessful import
59 public function testUIFImportUnsuccessful() {
60 $this->drupalLogin($this->import_user);
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()));
70 * Test import with core user fields.
72 class UIFTestCaseCore extends DrupalWebTestCase {
73 protected $admin_user;
74 protected $import_user;
76 public static function getInfo() {
78 'name' => 'Import {user} table fields',
79 'description' => 'Test UIF with core {user} table fields.',
80 'group' => 'User Import Framework',
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'));
90 public function testUIFImportCore() {
91 $this->drupalLogin($this->import_user);
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()));
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'));
117 // Retrieve imported user
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);
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"');
132 * Test import with Field module fields.
134 class UIFTestCaseFieldFields extends DrupalWebTestCase {
135 protected $admin_user;
136 protected $import_user;
138 public static function getInfo() {
140 'name' => 'Import Field module fields',
141 'description' => 'Test UIF with core Field module dependent fields.',
142 'group' => 'User Import Framework',
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'));
151 $vocabs = taxonomy_vocabulary_get_names();
152 $term = $this->createTerm($vocabs['tags'], 'granite');
156 * Test addition of all Field module fields.
158 public function testUIFImportWithFieldAllFields() {
161 'label' => 'Boolean Test',
162 'field_name' => 'boolean_test',
163 'type' => 'list_boolean',
164 'widget_type' => 'options_buttons',
165 'check_raw' => 'Boolean Test: </div><div class="field-items"><div class="field-item even">1</div>',
168 'label' => 'Decimal Test',
169 'field_name' => 'decimal_test',
170 'type' => 'number_decimal',
171 'widget_type' => 'number',
172 'check_text' => '3.14',
175 'label' => 'Float Test',
176 'field_name' => 'float_test',
177 'type' => 'number_float',
178 'widget_type' => 'number',
179 'check_text' => '1.23',
182 'label' => 'Integer Test',
183 'field_name' => 'integer_test',
184 'type' => 'number_integer',
185 'widget_type' => 'number',
186 'check_text' => '4 844 632',
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',
196 'label' => 'Integer List Test',
197 'field_name' => 'integer_list_test',
198 'type' => 'list_integer',
199 'widget_type' => 'options_select',
200 'check_text' => '5678',
203 'label' => 'Test List Test',
204 'field_name' => 'text_list_test',
205 'type' => 'list_text',
206 'widget_type' => 'options_select',
207 'check_text' => 'repeat',
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',
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',
224 'label' => 'Text Test',
225 'field_name' => 'text_test',
227 'widget_type' => 'text_textfield',
228 'check_text' => 'come to the aid',
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',
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'])));
248 if ($field['type'] == 'taxonomy_term_reference') {
249 // otherwise no vocab assigned
250 $this->drupalPost(NULL, array(), t('Save field settings'));
254 $this->drupalLogin($this->import_user);
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()));
264 // Retrieve imported user
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);
272 foreach ($fields as $field) {
273 $this->assertText($field['label']);
274 if (isset($field['check_text'])) {
275 $this->assertText($field['check_text']);
277 elseif (isset($field['check_raw'])) {
278 $this->assertRaw($field['check_raw']);
284 * Test addition of one field.
286 public function testUIFImportWithFieldOneField() {
287 $this->drupalLogin($this->admin_user);
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');
298 $this->drupalLogin($this->import_user);
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()));
314 // Retrieve imported user
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');
326 * Test unknown field.
328 public function testUIFImportWithFieldFieldsUnsuccessful() {
329 $this->drupalLogin($this->admin_user);
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]'])));
339 $this->drupalLogin($this->import_user);
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()));
348 * Returns a new vocabulary of some name.
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);
364 * Returns a new term with some name.
366 private function createTerm($vocabulary, $name) {
367 $term = new stdClass();
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);
380 * Test import with roles.
382 class UIFTestCaseRoles extends DrupalWebTestCase {
383 protected $admin_user;
384 protected $import_user;
386 public static function getInfo() {
388 'name' => 'Import with roles',
389 'description' => 'Test UIF with role import.',
390 'group' => 'User Import Framework',
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'));
403 public function testUIFImportWithRoles() {
404 $cook_rid = $this->drupalCreateRole(array(), 'cook');
405 $bottlewasher_rid = $this->drupalCreateRole(array(), 'bottlewasher');
407 $this->drupalLogin($this->import_user);
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()));
418 $this->drupalPost(NULL, array(), t('Import users'));
419 $this->assertText(t('User import complete', array()));
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);
427 $mail = 'bilbo@example.com';
428 $account = user_load_by_mail($mail);
429 $this->userLoadAndCheckRoleAssigned($account, $cook_rid);
430 $this->userLoadAndCheckRoleAssigned($account, $bottlewasher_rid);
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);
439 * Check role on user object.
441 * @param object $account
442 * The user account to check.
444 * The role ID to search for.
445 * @param bool $is_assigned
446 * (optional) Whether to assert that $rid exists (TRUE) or not (FALSE).
449 private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
450 $account = user_load($account->uid, TRUE);
452 $this->assertTrue(array_key_exists($rid, $account->roles), t('The role is present in the user object.'));
455 $this->assertFalse(array_key_exists($rid, $account->roles), t('The role is not present in the user object.'));