1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for plugins/FeedsUserProcessor.inc
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test aggregating a feed as data records.
|
10
|
*/
|
11
|
class FeedsCSVtoUsersTest extends FeedsWebTestCase {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'CSV import to users',
|
15
|
'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import users from a CSV file.',
|
16
|
'group' => 'Feeds',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Test node creation, refreshing/deleting feeds and feed items.
|
22
|
*/
|
23
|
public function test() {
|
24
|
// Create an importer.
|
25
|
$this->createImporterConfiguration('User import', 'user_import');
|
26
|
|
27
|
// Set and configure plugins.
|
28
|
$this->setPlugin('user_import', 'FeedsFileFetcher');
|
29
|
$this->setPlugin('user_import', 'FeedsCSVParser');
|
30
|
$this->setPlugin('user_import', 'FeedsUserProcessor');
|
31
|
|
32
|
// Go to mapping page and create a couple of mappings.
|
33
|
$mappings = array(
|
34
|
0 => array(
|
35
|
'source' => 'name',
|
36
|
'target' => 'name',
|
37
|
'unique' => FALSE,
|
38
|
),
|
39
|
1 => array(
|
40
|
'source' => 'mail',
|
41
|
'target' => 'mail',
|
42
|
'unique' => TRUE,
|
43
|
),
|
44
|
2 => array(
|
45
|
'source' => 'since',
|
46
|
'target' => 'created',
|
47
|
),
|
48
|
3 => array(
|
49
|
'source' => 'password',
|
50
|
'target' => 'pass',
|
51
|
),
|
52
|
);
|
53
|
$this->addMappings('user_import', $mappings);
|
54
|
|
55
|
// Use standalone form.
|
56
|
$edit = array(
|
57
|
'content_type' => '',
|
58
|
);
|
59
|
$this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save');
|
60
|
|
61
|
// Create roles and assign one of them to the users to be imported.
|
62
|
$manager_rid = $this->drupalCreateRole(array('access content'), 'manager');
|
63
|
$admin_rid = $this->drupalCreateRole(array('access content'), 'administrator');
|
64
|
$edit = array(
|
65
|
"roles[$manager_rid]" => TRUE,
|
66
|
"roles[$admin_rid]" => FALSE,
|
67
|
);
|
68
|
$this->setSettings('user_import', 'FeedsUserProcessor', $edit);
|
69
|
|
70
|
// Import CSV file.
|
71
|
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
72
|
|
73
|
// Assert result.
|
74
|
$this->assertText('Created 3 users');
|
75
|
// 1 user has an invalid email address, all users should be assigned
|
76
|
// the manager role.
|
77
|
$this->assertText('Failed importing 2 users.');
|
78
|
$this->drupalGet('admin/people');
|
79
|
$this->assertText('Morticia');
|
80
|
$this->assertText('Fester');
|
81
|
$this->assertText('Gomez');
|
82
|
$count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $manager_rid))->fetchField();
|
83
|
$this->assertEqual($count, 3, t('All imported users were assigned the manager role.'));
|
84
|
$count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $admin_rid))->fetchField();
|
85
|
$this->assertEqual($count, 0, t('No imported user was assigned the administrator role.'));
|
86
|
|
87
|
// Run import again, verify no new users.
|
88
|
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
89
|
$this->assertText('Failed importing 2 users.');
|
90
|
|
91
|
// Attempt to log in as one of the imported users.
|
92
|
$account = user_load_by_name('Morticia');
|
93
|
$this->assertTrue($account, 'Imported user account loaded.');
|
94
|
$account->pass_raw = 'mort';
|
95
|
$this->drupalLogin($account);
|
96
|
|
97
|
// Login as admin.
|
98
|
$this->drupalLogin($this->admin_user);
|
99
|
|
100
|
// Removing a mapping forces updating without needing a different file.
|
101
|
// We are also testing that if we don't map anything to the user's password
|
102
|
// that it will keep its existing one.
|
103
|
$mappings = array(
|
104
|
3 => array(
|
105
|
'source' => 'password',
|
106
|
'target' => 'pass',
|
107
|
),
|
108
|
);
|
109
|
$this->removeMappings('user_import', $mappings);
|
110
|
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2));
|
111
|
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
112
|
// Assert result.
|
113
|
$this->assertText('Updated 3 users');
|
114
|
$this->assertText('Failed importing 2 user');
|
115
|
|
116
|
// Attempt to log in as one of the imported users.
|
117
|
$account = user_load_by_name('Fester');
|
118
|
$this->assertTrue($account, 'Imported user account loaded.');
|
119
|
$account->pass_raw = 'fest';
|
120
|
$this->drupalLogin($account);
|
121
|
|
122
|
// Login as admin.
|
123
|
$this->drupalLogin($this->admin_user);
|
124
|
|
125
|
// Import modified CSV file, one (valid) user is missing.
|
126
|
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2, 'update_non_existent' => 'block'));
|
127
|
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users2.csv');
|
128
|
$this->assertText('Blocked 1 user');
|
129
|
$this->assertText('Failed importing 2 user');
|
130
|
|
131
|
// Import the original CSV file again.
|
132
|
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
133
|
$this->assertText('Updated 1 user');
|
134
|
$this->assertText('Failed importing 2 user');
|
135
|
}
|
136
|
|
137
|
}
|