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