1
|
<?php
|
2
|
// $Id: feeds_jsonpath_parser.test,v 1.1.2.1 2011/02/05 18:12:18 twistor Exp $
|
3
|
|
4
|
/**
|
5
|
* @file
|
6
|
* Tests for FeedsJSONPathParser.inc.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Test single feeds.
|
11
|
*/
|
12
|
class FeedsJSONPathParserTestCase extends FeedsWebTestCase {
|
13
|
|
14
|
/**
|
15
|
* Describe this test.
|
16
|
*/
|
17
|
public function getInfo() {
|
18
|
return array(
|
19
|
'name' => t('JSONPath Parser'),
|
20
|
'description' => t('Regression tests for Feeds JSONPath parser.'),
|
21
|
'group' => t('Feeds'),
|
22
|
);
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Set up test.
|
27
|
*/
|
28
|
public function setUp() {
|
29
|
parent::setUp(array('feeds', 'feeds_ui', 'ctools', 'job_scheduler', 'feeds_jsonpath_parser'));
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Run tests.
|
34
|
*/
|
35
|
public function test() {
|
36
|
$this->createImporterConfiguration('JSONPath', 'jsonpath');
|
37
|
|
38
|
$this->setPlugin('jsonpath', 'FeedsJSONPathParser');
|
39
|
$this->addMappings('jsonpath',
|
40
|
array(
|
41
|
array(
|
42
|
'source' => 'jsonpath_parser:0',
|
43
|
'target' => 'title',
|
44
|
'unique' => FALSE,
|
45
|
),
|
46
|
array(
|
47
|
'source' => 'jsonpath_parser:1',
|
48
|
'target' => 'url',
|
49
|
'unique' => TRUE,
|
50
|
),
|
51
|
)
|
52
|
);
|
53
|
// Set importer default settings.
|
54
|
$edit = array(
|
55
|
'jsonpath[context]' => '$.store.book[*]',
|
56
|
'jsonpath[sources][jsonpath_parser:0]' => 'author',
|
57
|
'jsonpath[sources][jsonpath_parser:1]' => 'price',
|
58
|
);
|
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');
|
64
|
|
65
|
// 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');
|
68
|
$this->assertText('Created 4 nodes');
|
69
|
|
70
|
// Import again, this verifies url field was mapped correctly.
|
71
|
$this->drupalPost('node/'. $nid .'/import', array(), 'Import');
|
72
|
$this->assertText('There are no new nodes');
|
73
|
|
74
|
// Assert accuracy of aggregated content. I find humor in using our own
|
75
|
// issue queue to run tests against.
|
76
|
$this->drupalGet('node');
|
77
|
$this->assertText('Nigel Rees');
|
78
|
$this->assertText('Evelyn Waugh');
|
79
|
$this->assertText('Herman Melville');
|
80
|
$this->assertText('J. R. R. Tolkien');
|
81
|
|
82
|
// Test that overriding default settings works.
|
83
|
$edit = array(
|
84
|
'feeds[FeedsJSONPathParser][jsonpath][context]' => '/foo',
|
85
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'bar',
|
86
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'baz',
|
87
|
);
|
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.
|
96
|
// That happened at one point.
|
97
|
$this->drupalPost('node/'. $nid .'/import', array(), 'Import');
|
98
|
$this->assertText('There are no new nodes');
|
99
|
|
100
|
// Put the values back so we can test inheritance if the form was changed
|
101
|
// and then changed back.
|
102
|
$edit = array(
|
103
|
'feeds[FeedsJSONPathParser][jsonpath][context]' => '$.store.book[*]',
|
104
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'author',
|
105
|
'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'price',
|
106
|
);
|
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');
|
112
|
|
113
|
// Change importer defaults.
|
114
|
$edit = array(
|
115
|
'jsonpath[context]' => '//tr',
|
116
|
'jsonpath[sources][jsonpath_parser:0]' => 'booya',
|
117
|
'jsonpath[sources][jsonpath_parser:1]' => 'boyz',
|
118
|
);
|
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');
|
124
|
|
125
|
// 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');
|
130
|
}
|
131
|
}
|