1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for FeedsJSONPathParser.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test single feeds.
|
10
|
*/
|
11
|
class FeedsJSONPathParserTestCase extends FeedsWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* Describe this test.
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => t('JSONPath Parser'),
|
19
|
'description' => t('Regression tests for Feeds JSONPath parser.'),
|
20
|
'group' => t('Feeds JSONPath Parser'),
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* Set up test.
|
26
|
*/
|
27
|
public function setUp() {
|
28
|
parent::setUp(array('feeds_jsonpath_parser'));
|
29
|
$this->downloadJsonPath();
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Downloads JSONPath.
|
34
|
*/
|
35
|
protected function downloadJsonPath() {
|
36
|
// We don't use a variable_set() here since we want this to be a unit test.
|
37
|
if (variable_get('feeds_jsonpath_library_dir')) {
|
38
|
return;
|
39
|
}
|
40
|
$url = 'https://jsonpath.googlecode.com/svn/trunk/src/php/jsonpath.php';
|
41
|
$filename = 'jsonpath.php';
|
42
|
|
43
|
// Avoid downloading the file dozens of times.
|
44
|
$library_dir = $this->originalFileDirectory . '/simpletest/feeds_jsonpath_parser';
|
45
|
$jsonpath_library_dir = DRUPAL_ROOT . '/' . $library_dir . '/jsonpath';
|
46
|
|
47
|
if (!file_exists(DRUPAL_ROOT . '/' . $library_dir)) {
|
48
|
drupal_mkdir(DRUPAL_ROOT . '/' . $library_dir);
|
49
|
}
|
50
|
|
51
|
if (!file_exists($jsonpath_library_dir)) {
|
52
|
drupal_mkdir($jsonpath_library_dir);
|
53
|
}
|
54
|
|
55
|
// Local file name.
|
56
|
$local_file = $jsonpath_library_dir . '/' . $filename;
|
57
|
|
58
|
// Begin single threaded code.
|
59
|
if (function_exists('sem_get')) {
|
60
|
$semaphore = sem_get(ftok(__FILE__, 1));
|
61
|
sem_acquire($semaphore);
|
62
|
}
|
63
|
|
64
|
// Download and extact the archive, but only in one thread.
|
65
|
if (!file_exists($local_file)) {
|
66
|
$local_file = system_retrieve_file($url, $local_file, FALSE, FILE_EXISTS_REPLACE);
|
67
|
}
|
68
|
|
69
|
if (function_exists('sem_get')) {
|
70
|
sem_release($semaphore);
|
71
|
}
|
72
|
|
73
|
// Verify that the file was successfully downloaded.
|
74
|
$this->assertTrue(file_exists($local_file), format_string('@file found.', array('@file' => $local_file)));
|
75
|
|
76
|
// Set the library directory.
|
77
|
variable_set('feeds_jsonpath_library_dir', $jsonpath_library_dir);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Run tests.
|
82
|
*/
|
83
|
public function test() {
|
84
|
$this->createImporterConfiguration('JSONPath', 'jsonpath');
|
85
|
|
86
|
$this->setPlugin('jsonpath', 'FeedsJSONPathParser');
|
87
|
$this->addMappings('jsonpath',
|
88
|
array(
|
89
|
array(
|
90
|
'source' => 'jsonpath_parser:0',
|
91
|
'target' => 'title',
|
92
|
'unique' => FALSE,
|
93
|
),
|
94
|
array(
|
95
|
'source' => 'jsonpath_parser:1',
|
96
|
'target' => 'url',
|
97
|
'unique' => TRUE,
|
98
|
),
|
99
|
)
|
100
|
);
|
101
|
// Set importer default settings.
|
102
|
$edit = array(
|
103
|
'jsonpath[context]' => '$.store.book[*]',
|
104
|
'jsonpath[sources][jsonpath_parser:0]' => 'author',
|
105
|
'jsonpath[sources][jsonpath_parser:1]' => 'price',
|
106
|
'jsonpath[allow_override]' => TRUE,
|
107
|
);
|
108
|
$edit_url = 'admin/structure/feeds/jsonpath/settings/FeedsJSONPathParser';
|
109
|
$node_save_text = 'Basic page Testing JSONPath Parser has been updated.';
|
110
|
|
111
|
$this->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.');
|
112
|
|
113
|
// Test import.
|
114
|
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_jsonpath_parser') . '/tests/feeds_jsonpath_parser';
|
115
|
$nid = $this->createFeedNode('jsonpath', $path . '/test.json', 'Testing JSONPath Parser');
|
116
|
$this->assertText('Created 4 nodes');
|
117
|
|
118
|
// Import again, this verifies url field was mapped correctly.
|
119
|
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
120
|
$this->assertText('There are no new nodes');
|
121
|
|
122
|
// Assert accuracy of aggregated content. I find humor in using our own
|
123
|
// issue queue to run tests against.
|
124
|
$this->drupalGet('node');
|
125
|
$this->assertText('Nigel Rees');
|
126
|
$this->assertText('Evelyn Waugh');
|
127
|
$this->assertText('Herman Melville');
|
128
|
$this->assertText('J. R. R. Tolkien');
|
129
|
|
130
|
// Test that overriding default settings works.
|
131
|
$edit = array(
|
132
|
'feeds[FeedsJSONPathParser][jsonpath][context]' => '/foo',
|
133
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'bar',
|
134
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'baz',
|
135
|
);
|
136
|
$this->postAndCheck('node/' . $nid . '/edit', $edit, 'Save', $node_save_text);
|
137
|
|
138
|
// Assert the we don't create an empty node when JSONPath values don't return anything.
|
139
|
// That happened at one point.
|
140
|
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
141
|
$this->assertText('There are no new nodes');
|
142
|
|
143
|
// Put the values back so we can test inheritance if the form was changed
|
144
|
// and then changed back.
|
145
|
$edit = array(
|
146
|
'feeds[FeedsJSONPathParser][jsonpath][context]' => '$.store.book[*]',
|
147
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'author',
|
148
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'price',
|
149
|
);
|
150
|
$this->postAndCheck('node/' . $nid . '/edit', $edit, 'Save', $node_save_text);
|
151
|
|
152
|
// Change importer defaults.
|
153
|
$edit = array(
|
154
|
'jsonpath[context]' => '//tr',
|
155
|
'jsonpath[sources][jsonpath_parser:0]' => 'booya',
|
156
|
'jsonpath[sources][jsonpath_parser:1]' => 'boyz',
|
157
|
);
|
158
|
$this->drupalPost($edit_url, $edit, 'Save');
|
159
|
$this->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.');
|
160
|
|
161
|
// Make sure the changes propigated.
|
162
|
$this->drupalGet('node/' . $nid . '/edit');
|
163
|
$this->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][context]', '//tr');
|
164
|
$this->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]', 'booya');
|
165
|
$this->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]', 'boyz');
|
166
|
|
167
|
// Turn off allow_override.
|
168
|
$edit = array(
|
169
|
'jsonpath[allow_override]' => FALSE,
|
170
|
);
|
171
|
$this->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.');
|
172
|
$this->drupalGet('node/' . $nid . '/edit');
|
173
|
$this->assertNoText('JSONPath Parser Settings');
|
174
|
|
175
|
// Test byte replacement.
|
176
|
$this->assertEqual('ࡇ abc ʦ 𡃁', FeedsJSONPathParser::convertFourBytes('ࡇ abc ʦ 𡃁'));
|
177
|
$this->assertEqual('ࡇ abc ʦ ', FeedsJSONPathParser::stripFourBytes('ࡇ abc ʦ 𡃁'));
|
178
|
}
|
179
|
|
180
|
public function postAndCheck($url, $edit, $button, $saved_text) {
|
181
|
$this->drupalPost($url, $edit, $button);
|
182
|
$this->assertText($saved_text);
|
183
|
$this->drupalGet($url);
|
184
|
foreach ($edit as $key => $value) {
|
185
|
$this->assertFieldByName($key, $value);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
}
|