Projet

Général

Profil

Paste
Télécharger (12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds_xpathparser / tests / feeds_xpathparser_parser_html.test @ a2bb1a14

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 &quot;body&quot;');
80
    $this->assertText('parsing link field');
81
    $this->assertText('Error when switching to XML Parser');
82
    $this->assertText('Duplicate content even if &quot;unique target&quot; 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('&lt;a href=&quot;http://drupal.org/node/976478&quot;&gt;Xpath Functions&lt;/a&gt;');
104
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1048030&quot;&gt;Unable to upload .html files&lt;/a&gt;');
105
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1050310&quot;&gt;Import to multiple content types&lt;/a&gt;');
106
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1047788&quot;&gt;Parser includes tags in mapped output&lt;/a&gt;');
107
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1043608&quot;&gt;Errors&lt;/a&gt;');
108
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1044546&quot;&gt;Loop through HTML - all data is in one node?&lt;/a&gt;');
109
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1043728&quot;&gt;Patch: add encoding options for PHP tidy feature&lt;/a&gt;');
110
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1040132&quot;&gt;Import and Maintain 1300+ Node Items&lt;/a&gt;');
111
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1043604&quot;&gt;Documentation update&lt;/a&gt;');
112
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1039492&quot;&gt;An HTTP error 404 occured&lt;/a&gt;');
113
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1042048&quot;&gt;Does it work with Feeds Image Grabber&lt;/a&gt;');
114
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/961158&quot;&gt;Node published date not being correctly mapped (set to 1 Jan 1970)&lt;/a&gt;');
115
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1021474&quot;&gt;fields to fill xpath not displayed in importer interface except for &quot;body&quot;&lt;/a&gt;');
116
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1040530&quot;&gt;parsing link field&lt;/a&gt;');
117
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1038912&quot;&gt;Error when switching to XML Parser&lt;/a&gt;');
118
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1032340&quot;&gt;Duplicate content even if &quot;unique target&quot; is set&lt;/a&gt;');
119
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/982102&quot;&gt;Labels/field names become meaningless with Data Processor&lt;/a&gt;');
120
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/1034758&quot;&gt;Xpath namespace help&lt;/a&gt;');
121
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/908458&quot;&gt;warning: mysql_real_escape_string()&lt;/a&gt;');
122
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/869076&quot;&gt;Feeds XPath Parser: warning: Invalid argument&lt;/a&gt;');
123
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/991386&quot;&gt;What am I missing? FeedsXPathParser: No mappings are defined.&lt;/a&gt;');
124
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/990972&quot;&gt;CDATA in tag not producing text&lt;/a&gt;');
125
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/989948&quot;&gt;Cant map empty fields&lt;/a&gt;');
126
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/973324&quot;&gt;Support literal XPath expressions&lt;/a&gt;');
127
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/958344&quot;&gt;adding a prefix to a parsed xml value.&lt;/a&gt;');
128
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/914216&quot;&gt;Mapping on import&lt;/a&gt;');
129
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/863714&quot;&gt;Feeds XPath Parser: HTML parser example for number expressions&lt;/a&gt;');
130
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/915856&quot;&gt;I dont want to define any field queries&lt;/a&gt;');
131
    $this->assertText('&lt;a href=&quot;http://drupal.org/node/950150&quot;&gt;Document // and other syntax for this module a little better&lt;/a&gt;');
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
}