1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for FeedsXPathParser.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test single feeds.
|
10
|
*/
|
11
|
class FeedsXPathParseHTMLTestCase extends FeedsXPathParserWebTestCase {
|
12
|
|
13
|
/**
|
14
|
* Describe this test.
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => 'HTML Parser',
|
19
|
'description' => 'Regression tests for Feeds XPath HTML parser.',
|
20
|
'group' => 'Feeds XPath Parser',
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* Run tests.
|
26
|
*/
|
27
|
public function test() {
|
28
|
$this->createImporterConfiguration('XPath', 'xpath');
|
29
|
|
30
|
$this->setPlugin('xpath', 'FeedsXPathParserHTML');
|
31
|
$this->addMappings('xpath', array(
|
32
|
0 => array(
|
33
|
'source' => 'xpathparser:0',
|
34
|
'target' => 'title',
|
35
|
'unique' => FALSE,
|
36
|
),
|
37
|
1 => array(
|
38
|
'source' => 'xpathparser:1',
|
39
|
'target' => 'url',
|
40
|
'unique' => TRUE,
|
41
|
),
|
42
|
));
|
43
|
// Set importer default settings.
|
44
|
$importer_url = $this->feeds_base . '/xpath/settings/FeedsXPathParserHTML';
|
45
|
$edit = array(
|
46
|
'xpath[context]' => '//tr[starts-with(@class, "odd ") or starts-with(@class, "even ")]',
|
47
|
'xpath[sources][xpathparser:0]' => 'td[1]/a',
|
48
|
'xpath[sources][xpathparser:1]' => 'td[1]/a/@href',
|
49
|
);
|
50
|
$this->postAndCheck($importer_url, $edit, t('Save'), t('Your changes have been saved.'));
|
51
|
|
52
|
// Test import.
|
53
|
// Set batch limit to 5 to force batching.
|
54
|
variable_set('feeds_process_limit', 5);
|
55
|
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_xpathparser') . '/tests/feeds_xpathparser/';
|
56
|
$nid = $this->createFeedNode('xpath', $path . 'issues_drupal.org.htm', 'Testing XPath HTML Parser');
|
57
|
$feed_node_edit_url = 'node/' . $nid . '/edit';
|
58
|
$this->assertText(t('Created 29 nodes'));
|
59
|
|
60
|
// Import again, this verifies url field was mapped correctly.
|
61
|
$this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
|
62
|
$this->assertText(t('There are no new nodes'));
|
63
|
|
64
|
// Assert accuracy of aggregated content. I find humor in using our own
|
65
|
// issue queue to run tests against.
|
66
|
$this->drupalGet('node');
|
67
|
$this->assertText('Xpath Functions');
|
68
|
$this->assertText('Unable to upload .html files');
|
69
|
$this->assertText('Import to multiple content types');
|
70
|
$this->assertText('Parser includes tags in mapped output');
|
71
|
$this->assertText('Errors');
|
72
|
$this->assertText('Loop through HTML - all data is in one node?');
|
73
|
$this->assertText('Patch: add encoding options for PHP tidy feature');
|
74
|
$this->assertText('Import and Maintain 1300+ Node Items');
|
75
|
$this->assertText('Documentation update');
|
76
|
$this->assertText('An HTTP error 404 occured');
|
77
|
$this->assertText('Does it work with Feeds Image Grabber');
|
78
|
$this->assertText('Node published date not being correctly mapped (set to 1 Jan 1970)');
|
79
|
$this->assertText('fields to fill xpath not displayed in importer interface except for "body"');
|
80
|
$this->assertText('parsing link field');
|
81
|
$this->assertText('Error when switching to XML Parser');
|
82
|
$this->assertText('Duplicate content even if "unique target" is set');
|
83
|
$this->assertText('Labels/field names become meaningless with Data Processor');
|
84
|
$this->assertText('Xpath namespace help');
|
85
|
$this->assertText('warning: mysql_real_escape_string()');
|
86
|
$this->assertText('Feeds XPath Parser: warning: Invalid argument');
|
87
|
$this->assertText('What am I missing? FeedsXPathParser: No mappings are defined.');
|
88
|
$this->assertText('CDATA in tag not producing text');
|
89
|
$this->assertText('Cant map empty fields');
|
90
|
$this->assertText('Support literal XPath expressions');
|
91
|
$this->assertText('adding a prefix to a parsed xml value.');
|
92
|
$this->assertText('Mapping on import');
|
93
|
$this->assertText('Feeds XPath Parser: HTML parser example for number expressions');
|
94
|
$this->assertText("I dont want to define any field queries");
|
95
|
$this->assertText("Document // and other syntax for this module a little better");
|
96
|
|
97
|
// Test debugging.
|
98
|
$edit = array(
|
99
|
'feeds[FeedsXPathParserHTML][xpath][exp][debug][xpathparser:0]' => TRUE,
|
100
|
);
|
101
|
$this->postAndCheck($feed_node_edit_url, $edit, t('Save'), 'Basic page Testing XPath HTML Parser has been updated.');
|
102
|
$this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
|
103
|
$this->assertText('<a href="http://drupal.org/node/976478">Xpath Functions</a>');
|
104
|
$this->assertText('<a href="http://drupal.org/node/1048030">Unable to upload .html files</a>');
|
105
|
$this->assertText('<a href="http://drupal.org/node/1050310">Import to multiple content types</a>');
|
106
|
$this->assertText('<a href="http://drupal.org/node/1047788">Parser includes tags in mapped output</a>');
|
107
|
$this->assertText('<a href="http://drupal.org/node/1043608">Errors</a>');
|
108
|
$this->assertText('<a href="http://drupal.org/node/1044546">Loop through HTML - all data is in one node?</a>');
|
109
|
$this->assertText('<a href="http://drupal.org/node/1043728">Patch: add encoding options for PHP tidy feature</a>');
|
110
|
$this->assertText('<a href="http://drupal.org/node/1040132">Import and Maintain 1300+ Node Items</a>');
|
111
|
$this->assertText('<a href="http://drupal.org/node/1043604">Documentation update</a>');
|
112
|
$this->assertText('<a href="http://drupal.org/node/1039492">An HTTP error 404 occured</a>');
|
113
|
$this->assertText('<a href="http://drupal.org/node/1042048">Does it work with Feeds Image Grabber</a>');
|
114
|
$this->assertText('<a href="http://drupal.org/node/961158">Node published date not being correctly mapped (set to 1 Jan 1970)</a>');
|
115
|
$this->assertText('<a href="http://drupal.org/node/1021474">fields to fill xpath not displayed in importer interface except for "body"</a>');
|
116
|
$this->assertText('<a href="http://drupal.org/node/1040530">parsing link field</a>');
|
117
|
$this->assertText('<a href="http://drupal.org/node/1038912">Error when switching to XML Parser</a>');
|
118
|
$this->assertText('<a href="http://drupal.org/node/1032340">Duplicate content even if "unique target" is set</a>');
|
119
|
$this->assertText('<a href="http://drupal.org/node/982102">Labels/field names become meaningless with Data Processor</a>');
|
120
|
$this->assertText('<a href="http://drupal.org/node/1034758">Xpath namespace help</a>');
|
121
|
$this->assertText('<a href="http://drupal.org/node/908458">warning: mysql_real_escape_string()</a>');
|
122
|
$this->assertText('<a href="http://drupal.org/node/869076">Feeds XPath Parser: warning: Invalid argument</a>');
|
123
|
$this->assertText('<a href="http://drupal.org/node/991386">What am I missing? FeedsXPathParser: No mappings are defined.</a>');
|
124
|
$this->assertText('<a href="http://drupal.org/node/990972">CDATA in tag not producing text</a>');
|
125
|
$this->assertText('<a href="http://drupal.org/node/989948">Cant map empty fields</a>');
|
126
|
$this->assertText('<a href="http://drupal.org/node/973324">Support literal XPath expressions</a>');
|
127
|
$this->assertText('<a href="http://drupal.org/node/958344">adding a prefix to a parsed xml value.</a>');
|
128
|
$this->assertText('<a href="http://drupal.org/node/914216">Mapping on import</a>');
|
129
|
$this->assertText('<a href="http://drupal.org/node/863714">Feeds XPath Parser: HTML parser example for number expressions</a>');
|
130
|
$this->assertText('<a href="http://drupal.org/node/915856">I dont want to define any field queries</a>');
|
131
|
$this->assertText('<a href="http://drupal.org/node/950150">Document // and other syntax for this module a little better</a>');
|
132
|
$this->assertText(t('There are no new nodes'));
|
133
|
// Turn debugging off.
|
134
|
$edit = array(
|
135
|
'feeds[FeedsXPathParserHTML][xpath][exp][debug][xpathparser:0]' => FALSE,
|
136
|
);
|
137
|
$this->postAndCheck($feed_node_edit_url, $edit, t('Save'), 'Basic page Testing XPath HTML Parser has been updated.');
|
138
|
|
139
|
// Test that overriding default settings works.
|
140
|
$edit = array(
|
141
|
'feeds[FeedsXPathParserHTML][xpath][context]' => '/foo',
|
142
|
'feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:0]' => 'bar',
|
143
|
'feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:1]' => 'baz',
|
144
|
);
|
145
|
|
146
|
$this->postAndCheck($feed_node_edit_url, $edit, t('Save'), 'Basic page Testing XPath HTML Parser has been updated.');
|
147
|
|
148
|
// Assert the we don't create an empty node when XPath values don't return anything.
|
149
|
// That happened at one point.
|
150
|
$this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
|
151
|
$this->assertText(t('There are no new nodes'));
|
152
|
|
153
|
// Test that validation works.
|
154
|
$edit = array(
|
155
|
'feeds[FeedsXPathParserHTML][xpath][context]' => 'sdf asf',
|
156
|
'feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:0]' => 'asdf[sadfas asdf]',
|
157
|
);
|
158
|
$this->drupalPost($feed_node_edit_url, $edit, 'Save');
|
159
|
// Check for valid error messages.
|
160
|
$this->assertText('There was an error with the XPath selector: Invalid expression');
|
161
|
$this->assertText('There was an error with the XPath selector: Invalid predicate');
|
162
|
// Make sure the fields are errored out correctly. I.e. we have red outlines.
|
163
|
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsxpathparserhtml-xpath-context"][1]/@class', 'form-text required error');
|
164
|
$this->assertFieldByXPath('//input[@id="edit-feeds-feedsxpathparserhtml-xpath-sources-xpathparser0"][1]/@class', 'form-text error');
|
165
|
|
166
|
// Put the values back so we can test inheritance if the form was changed
|
167
|
// and then changed back.
|
168
|
$edit = array(
|
169
|
'feeds[FeedsXPathParserHTML][xpath][context]' => '//tr[starts-with(@class, "odd ") or starts-with(@class, "even ")]',
|
170
|
'feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:0]' => 'td[1]/a',
|
171
|
'feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:1]' => 'td[1]/a/@href',
|
172
|
);
|
173
|
$this->postAndCheck($feed_node_edit_url, $edit, t('Save'), t('Basic page Testing XPath HTML Parser has been updated.'));
|
174
|
|
175
|
// Change importer defaults.
|
176
|
$edit = array(
|
177
|
'xpath[context]' => '//tr',
|
178
|
'xpath[sources][xpathparser:0]' => 'booya',
|
179
|
'xpath[sources][xpathparser:1]' => 'boyz',
|
180
|
);
|
181
|
$this->postAndCheck($importer_url, $edit, t('Save'), t('Your changes have been saved.'));
|
182
|
|
183
|
// Make sure the changes propigated.
|
184
|
$this->drupalGet($feed_node_edit_url);
|
185
|
$this->assertFieldByName('feeds[FeedsXPathParserHTML][xpath][context]', '//tr');
|
186
|
$this->assertFieldByName('feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:0]', 'booya');
|
187
|
$this->assertFieldByName('feeds[FeedsXPathParserHTML][xpath][sources][xpathparser:1]', 'boyz');
|
188
|
|
189
|
//Cleanup
|
190
|
$this->drupalPost("node/$nid/delete-items", array(), t('Delete'));
|
191
|
$this->assertText(t('Deleted 29 nodes'));
|
192
|
|
193
|
$this->_testGetRaw($importer_url);
|
194
|
}
|
195
|
|
196
|
|
197
|
public function _testGetRaw($importer_url) {
|
198
|
$this->addMappings('xpath', array(
|
199
|
2 => array(
|
200
|
'source' => 'xpathparser:2',
|
201
|
'target' => 'body',
|
202
|
),
|
203
|
));
|
204
|
// Change importer defaults.
|
205
|
$edit = array(
|
206
|
'xpath[context]' => '/html',
|
207
|
'xpath[sources][xpathparser:0]' => 'head/title',
|
208
|
'xpath[sources][xpathparser:2]' => 'body',
|
209
|
'xpath[rawXML][xpathparser:2]' => TRUE,
|
210
|
);
|
211
|
$this->postAndCheck($importer_url, $edit, t('Save'), t('Your changes have been saved.'));
|
212
|
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_xpathparser') . '/tests/feeds_xpathparser/';
|
213
|
$nid = $this->createFeedNode('xpath', $path . 'simple.html', 'Testing GetRaw');
|
214
|
$feed_node_edit_url = "node/$nid/edit";
|
215
|
$this->assertText(t('Created 1 node'));
|
216
|
$url = 'node/' . ++$nid . '/edit';
|
217
|
$this->drupalGet($url);
|
218
|
$this->assertFieldByName('body[und][0][value]', '<body><div>bla bla</div></body>');
|
219
|
}
|
220
|
|
221
|
}
|