Révision 7707c013
Ajouté par Assos Assos il y a presque 9 ans
drupal7/sites/all/modules/feeds_jsonpath_parser/tests/feeds_jsonpath_parser.test | ||
---|---|---|
1 | 1 |
<?php |
2 |
// $Id: feeds_jsonpath_parser.test,v 1.1.2.1 2011/02/05 18:12:18 twistor Exp $ |
|
3 | 2 |
|
4 | 3 |
/** |
5 | 4 |
* @file |
... | ... | |
14 | 13 |
/** |
15 | 14 |
* Describe this test. |
16 | 15 |
*/ |
17 |
public function getInfo() { |
|
16 |
public static function getInfo() {
|
|
18 | 17 |
return array( |
19 | 18 |
'name' => t('JSONPath Parser'), |
20 | 19 |
'description' => t('Regression tests for Feeds JSONPath parser.'), |
21 |
'group' => t('Feeds'), |
|
20 |
'group' => t('Feeds JSONPath Parser'),
|
|
22 | 21 |
); |
23 | 22 |
} |
24 | 23 |
|
... | ... | |
26 | 25 |
* Set up test. |
27 | 26 |
*/ |
28 | 27 |
public function setUp() { |
29 |
parent::setUp(array('feeds', 'feeds_ui', 'ctools', 'job_scheduler', 'feeds_jsonpath_parser')); |
|
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); |
|
30 | 78 |
} |
31 | 79 |
|
32 | 80 |
/** |
... | ... | |
55 | 103 |
'jsonpath[context]' => '$.store.book[*]', |
56 | 104 |
'jsonpath[sources][jsonpath_parser:0]' => 'author', |
57 | 105 |
'jsonpath[sources][jsonpath_parser:1]' => 'price', |
106 |
'jsonpath[allow_override]' => TRUE, |
|
58 | 107 |
); |
59 |
$this->drupalPost('admin/structure/feeds/edit/jsonpath/settings/FeedsJSONPathParser', $edit, 'Save'); |
|
60 |
$this->assertText('Your changes have been saved.'); |
|
61 |
$this->assertRaw('$.store.book[*]'); |
|
62 |
$this->assertRaw('author'); |
|
63 |
$this->assertRaw('price'); |
|
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.'); |
|
64 | 112 |
|
65 | 113 |
// Test import. |
66 |
$path = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds_jsonpath_parser') .'/tests/feeds_jsonpath_parser/';
|
|
67 |
$nid = $this->createFeedNode('jsonpath', $path . 'test.json', 'Testing JSONPath Parser'); |
|
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');
|
|
68 | 116 |
$this->assertText('Created 4 nodes'); |
69 | 117 |
|
70 | 118 |
// Import again, this verifies url field was mapped correctly. |
71 |
$this->drupalPost('node/'. $nid .'/import', array(), 'Import');
|
|
119 |
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
|
72 | 120 |
$this->assertText('There are no new nodes'); |
73 | 121 |
|
74 | 122 |
// Assert accuracy of aggregated content. I find humor in using our own |
... | ... | |
85 | 133 |
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'bar', |
86 | 134 |
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'baz', |
87 | 135 |
); |
88 |
$this->drupalPost('node/'. $nid .'/edit', $edit, 'Save'); |
|
89 |
$this->drupalGet('node/'. $nid .'/edit'); |
|
90 |
// Why not use XPath to test JSONPath? |
|
91 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-context"][1]/@value', '/foo'); |
|
92 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser0"][1]/@value', 'bar'); |
|
93 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser1"][1]/@value', 'baz'); |
|
94 |
|
|
95 |
// Assert the we don't create an empty node when XPath values don't return anything. |
|
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. |
|
96 | 139 |
// That happened at one point. |
97 |
$this->drupalPost('node/'. $nid .'/import', array(), 'Import');
|
|
140 |
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
|
98 | 141 |
$this->assertText('There are no new nodes'); |
99 | 142 |
|
100 | 143 |
// Put the values back so we can test inheritance if the form was changed |
... | ... | |
104 | 147 |
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'author', |
105 | 148 |
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'price', |
106 | 149 |
); |
107 |
$this->drupalPost('node/'. $nid .'/edit', $edit, 'Save'); |
|
108 |
$this->drupalGet('node/'. $nid .'/edit'); |
|
109 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-context"][1]/@value', '$.store.book[*]'); |
|
110 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser0"][1]/@value', 'author'); |
|
111 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser1"][1]/@value', 'price'); |
|
150 |
$this->postAndCheck('node/' . $nid . '/edit', $edit, 'Save', $node_save_text); |
|
112 | 151 |
|
113 | 152 |
// Change importer defaults. |
114 | 153 |
$edit = array( |
... | ... | |
116 | 155 |
'jsonpath[sources][jsonpath_parser:0]' => 'booya', |
117 | 156 |
'jsonpath[sources][jsonpath_parser:1]' => 'boyz', |
118 | 157 |
); |
119 |
$this->drupalPost('admin/structure/feeds/edit/jsonpath/settings/FeedsJSONPathParser', $edit, 'Save'); |
|
120 |
$this->assertText('Your changes have been saved.'); |
|
121 |
$this->assertRaw('//tr'); |
|
122 |
$this->assertRaw('booya'); |
|
123 |
$this->assertRaw('boyz'); |
|
158 |
$this->drupalPost($edit_url, $edit, 'Save'); |
|
159 |
$this->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.'); |
|
124 | 160 |
|
125 | 161 |
// Make sure the changes propigated. |
126 |
$this->drupalGet('node/'. $nid .'/edit'); |
|
127 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-context"][1]/@value', '//tr'); |
|
128 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser0"][1]/@value', 'booya'); |
|
129 |
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsjsonpathparser-jsonpath-sources-jsonpath-parser1"][1]/@value', 'boyz'); |
|
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 ʦ 𡃁')); |
|
130 | 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 |
|
|
131 | 189 |
} |
Formats disponibles : Unified diff
Update Feeds JSONPath Parser 7.x-1.0-beta2 -> 7.x-1.0