1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Test case for simple CCK field mapper mappers/content.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Class for testing Feeds field mapper.
|
10
|
*/
|
11
|
class FeedsMapperFieldTestCase extends DrupalWebTestCase{
|
12
|
/**
|
13
|
* Test info function.
|
14
|
*/
|
15
|
public static function getInfo() {
|
16
|
return array(
|
17
|
'name' => 'Feeds integration (field mapper)',
|
18
|
'description' => 'Test Feeds Mapper support for fields.',
|
19
|
'group' => 'Entity Reference',
|
20
|
);
|
21
|
}
|
22
|
|
23
|
/**
|
24
|
* Set-up function.
|
25
|
*/
|
26
|
public function setUp() {
|
27
|
parent::setUp();
|
28
|
module_enable(array('entityreference_feeds_test'), TRUE);
|
29
|
$this->resetAll();
|
30
|
|
31
|
if (!module_exists('feeds')) {
|
32
|
return;
|
33
|
}
|
34
|
|
35
|
$permissions[] = 'access content';
|
36
|
$permissions[] = 'administer site configuration';
|
37
|
$permissions[] = 'administer content types';
|
38
|
$permissions[] = 'administer nodes';
|
39
|
$permissions[] = 'bypass node access';
|
40
|
$permissions[] = 'administer taxonomy';
|
41
|
$permissions[] = 'administer users';
|
42
|
$permissions[] = 'administer feeds';
|
43
|
|
44
|
// Create an admin user and log in.
|
45
|
$this->admin_user = $this->drupalCreateUser($permissions);
|
46
|
$this->drupalLogin($this->admin_user);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Check if mapping exists.
|
51
|
*
|
52
|
* @param string $id
|
53
|
* ID of the importer.
|
54
|
* @param integer $i
|
55
|
* The key of the mapping.
|
56
|
* @param string $source
|
57
|
* The source field.
|
58
|
* @param string $target
|
59
|
* The target field.
|
60
|
*
|
61
|
* @return integer
|
62
|
* -1 if the mapping doesn't exist, the key of the mapping otherwise.
|
63
|
*/
|
64
|
public function mappingExists($id, $i, $source, $target) {
|
65
|
|
66
|
$current_mappings = $this->getCurrentMappings($id);
|
67
|
|
68
|
if ($current_mappings) {
|
69
|
foreach ($current_mappings as $key => $mapping) {
|
70
|
if ($mapping['source'] == $source && $mapping['target'] == $target && $key == $i) {
|
71
|
return $key;
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
|
76
|
return -1;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Adds mappings to a given configuration.
|
81
|
*
|
82
|
* @param string $id
|
83
|
* ID of the importer.
|
84
|
* @param array $mappings
|
85
|
* An array of mapping arrays. Each mapping array must have a source and
|
86
|
* an target key and can have a unique key.
|
87
|
* @param bool $test_mappings
|
88
|
* (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
|
89
|
*/
|
90
|
public function addMappings($id, $mappings, $test_mappings = TRUE) {
|
91
|
|
92
|
$path = "admin/structure/feeds/$id/mapping";
|
93
|
|
94
|
// Iterate through all mappings and add the mapping via the form.
|
95
|
foreach ($mappings as $i => $mapping) {
|
96
|
|
97
|
if ($test_mappings) {
|
98
|
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
|
99
|
$this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.');
|
100
|
}
|
101
|
|
102
|
// Get unique flag and unset it. Otherwise, drupalPost will complain that
|
103
|
// Split up config and mapping.
|
104
|
$config = $mapping;
|
105
|
unset($config['source'], $config['target']);
|
106
|
$mapping = array('source' => $mapping['source'], 'target' => $mapping['target']);
|
107
|
|
108
|
// Add mapping.
|
109
|
$this->drupalPost($path, $mapping, t('Add'));
|
110
|
|
111
|
// If there are other configuration options, set them.
|
112
|
if ($config) {
|
113
|
$this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_' . $i);
|
114
|
|
115
|
// Set some settings.
|
116
|
$edit = array();
|
117
|
foreach ($config as $key => $value) {
|
118
|
$edit["config[$i][settings][$key]"] = $value;
|
119
|
}
|
120
|
$this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
|
121
|
$this->drupalPost(NULL, array(), t('Save'));
|
122
|
}
|
123
|
|
124
|
if ($test_mappings) {
|
125
|
$current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
|
126
|
$this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition.');
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Gets an array of current mappings from the feeds_importer config.
|
133
|
*
|
134
|
* @param string $id
|
135
|
* ID of the importer.
|
136
|
*
|
137
|
* @return bool|array
|
138
|
* FALSE if the importer has no mappings, or an an array of mappings.
|
139
|
*/
|
140
|
public function getCurrentMappings($id) {
|
141
|
$config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField();
|
142
|
|
143
|
$config = unserialize($config);
|
144
|
|
145
|
// We are very specific here. 'mappings' can either be an array or not
|
146
|
// exist.
|
147
|
if (array_key_exists('mappings', $config['processor']['config'])) {
|
148
|
$this->assertTrue(is_array($config['processor']['config']['mappings']), 'Mappings is an array.');
|
149
|
|
150
|
return $config['processor']['config']['mappings'];
|
151
|
}
|
152
|
|
153
|
return FALSE;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Basic test loading a double entry CSV file.
|
158
|
*/
|
159
|
public function test() {
|
160
|
if (!module_exists('feeds')) {
|
161
|
return;
|
162
|
}
|
163
|
|
164
|
$this->drupalLogin($this->admin_user);
|
165
|
$this->drupalGet('admin/structure/types/manage/article/fields');
|
166
|
$this->assertText('Ref - entity ID', t('Found Entity reference field %field.', array('%field' => 'field_er_id')));
|
167
|
$this->assertText('Ref - entity label', t('Found Entity reference field %field.', array('%field' => 'field_er_label')));
|
168
|
$this->assertText('Ref - feeds GUID', t('Found Entity reference field %field.', array('%field' => 'field_er_guid')));
|
169
|
$this->assertText('Ref - feeds URL', t('Found Entity reference field %field.', array('%field' => 'field_er_url')));
|
170
|
|
171
|
// Add feeds importer
|
172
|
$this->drupalGet('admin/structure/feeds');
|
173
|
$this->clickLink('Add importer');
|
174
|
$this->drupalPost('admin/structure/feeds/create', array('name' => 'Nodes', 'id' => 'nodes'), 'Create');
|
175
|
$this->assertText('Your configuration has been created with default settings.');
|
176
|
|
177
|
$this->drupalPost('admin/structure/feeds/nodes/settings/', array('content_type' => '', 'import_period' => -1), 'Save');
|
178
|
$this->assertText('Your changes have been saved.');
|
179
|
|
180
|
$this->drupalPost("admin/structure/feeds/nodes/fetcher", array('plugin_key' => 'FeedsFileFetcher'), 'Save');
|
181
|
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
|
182
|
$this->assertEqual($config['fetcher']['plugin_key'], 'FeedsFileFetcher', 'Verified correct fetcher (FeedsFileFetcher).');
|
183
|
|
184
|
$this->drupalPost("admin/structure/feeds/nodes/parser", array('plugin_key' => 'FeedsCSVParser'), 'Save');
|
185
|
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
|
186
|
$this->assertEqual($config['parser']['plugin_key'], 'FeedsCSVParser', 'Verified correct parser (FeedsCSVParser).');
|
187
|
|
188
|
$this->drupalPost('admin/structure/feeds/nodes/settings/FeedsNodeProcessor', array('content_type' => 'article'), 'Save');
|
189
|
$this->assertText('Your changes have been saved.');
|
190
|
|
191
|
$this->addMappings('nodes', array(
|
192
|
0 => array(
|
193
|
'source' => 'title',
|
194
|
'target' => 'title',
|
195
|
),
|
196
|
1 => array(
|
197
|
'source' => 'nid',
|
198
|
'target' => 'nid',
|
199
|
'unique' => TRUE,
|
200
|
),
|
201
|
2 => array(
|
202
|
'source' => 'permalink',
|
203
|
'target' => 'url',
|
204
|
'unique' => TRUE,
|
205
|
),
|
206
|
3 => array(
|
207
|
'source' => 'nid',
|
208
|
'target' => 'guid',
|
209
|
'unique' => TRUE,
|
210
|
),
|
211
|
4 => array(
|
212
|
'source' => 'parent_nid',
|
213
|
'target' => 'field_er_id:etid',
|
214
|
),
|
215
|
5 => array(
|
216
|
'source' => 'parent_title',
|
217
|
'target' => 'field_er_label:label',
|
218
|
),
|
219
|
6 => array(
|
220
|
'source' => 'parent_url',
|
221
|
'target' => 'field_er_url:url',
|
222
|
),
|
223
|
7 => array(
|
224
|
'source' => 'parent_guid',
|
225
|
'target' => 'field_er_guid',
|
226
|
),
|
227
|
)
|
228
|
);
|
229
|
|
230
|
$file = realpath(getcwd()) . '/' . drupal_get_path('module', 'entityreference') . '/tests/feeds_test.csv';
|
231
|
$this->assertTrue(file_exists($file), 'Source file exists');
|
232
|
|
233
|
$this->drupalPost('import/nodes', array('files[feeds]' => $file), 'Import');
|
234
|
$this->assertText('Created 2 nodes');
|
235
|
|
236
|
$parent = node_load(1);
|
237
|
$this->assertTrue(empty($parent->field_er_id['und'][0]['target_id']), t('Parent node: Import by entity ID OK.'));
|
238
|
$this->assertTrue(empty($parent->field_er_label['und'][0]['target_id']), t('Parent node: Import by entity label OK.'));
|
239
|
$this->assertTrue(empty($parent->field_er_guid['und'][0]['target_id']), t('Parent node: Import by feeds GUID OK.'));
|
240
|
$this->assertTrue(empty($parent->field_er_url['und'][0]['target_id']), t('Parent node: Import by feeds URL OK.'));
|
241
|
|
242
|
$child = node_load(2);
|
243
|
$this->assertTrue($child->field_er_id['und'][0]['target_id'] == 1, t('Child node: Import by entity ID OK.'));
|
244
|
$this->assertTrue($child->field_er_label['und'][0]['target_id'] == 1, t('Child node: Import by entity label OK.'));
|
245
|
$this->assertTrue($child->field_er_guid['und'][0]['target_id'] == 1, t('Child node: Import by feeds GUID OK.'));
|
246
|
$this->assertTrue($child->field_er_url['und'][0]['target_id'] == 1, t('Child node: Import by feeds URL OK.'));
|
247
|
}
|
248
|
}
|