You are here

public function XPathHTMLParserTest::test in Feeds XPath Parser 8

Run tests.

File

lib/Drupal/feeds_xpathparser/Tests/XPathHTMLParserTest.php, line 28
Contains Drupal\feeds_xpathparser\Tests\XPathHTMLParserTest.

Class

XPathHTMLParserTest
Test single feeds.

Namespace

Drupal\feeds_xpathparser\Tests

Code

public function test() {
  $this
    ->createImporterConfiguration('XPath', 'xpath');
  $this
    ->setPlugin('xpath', 'parser', 'feeds_xpathparser_html');
  $this
    ->addMappings('xpath', array(
    0 => array(
      'source' => 'xpathparser:0',
      'target' => 'title',
      'unique' => FALSE,
    ),
    1 => array(
      'source' => 'xpathparser:1',
      'target' => 'url',
      'unique' => TRUE,
    ),
  ));

  // Set importer default settings.
  $importer_url = self::FEEDS_BASE . '/xpath/settings/parser';
  $edit = array(
    'context' => '//tr[starts-with(@class, "odd ") or starts-with(@class, "even ")]',
    'sources[xpathparser:0]' => 'td[1]/a',
    'sources[xpathparser:1]' => 'td[1]/a/@href',
    'allow_override' => TRUE,
  );
  $this
    ->postAndCheck($importer_url, $edit, t('Save'), t('Your changes have been saved.'));

  // Test import.
  // Set batch limit to 5 to force batching.
  variable_set('feeds_process_limit', 5);
  $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_xpathparser') . '/tests/';
  $fid = $this
    ->createFeed('xpath', $path . 'issues_drupal.org.htm', 'Testing XPath HTML Parser');
  $feed_edit_url = 'feed/' . $fid . '/edit';
  $this
    ->assertText(t('Created 29 nodes'));

  // Import again, this verifies url field was mapped correctly.
  $this
    ->feedImportItems($fid);
  $this
    ->assertText(t('There are no new nodes'));

  // Assert accuracy of aggregated content. I find humor in using our own
  // issue queue to run tests against.
  $this
    ->drupalGet('node');
  $this
    ->assertText('Xpath Functions');
  $this
    ->assertText('Unable to upload .html files');
  $this
    ->assertText('Import to multiple content types');
  $this
    ->assertText('Parser includes tags in mapped output');
  $this
    ->assertText('Errors');
  $this
    ->assertText('Loop through HTML - all data is in one node?');
  $this
    ->assertText('Patch: add encoding options for PHP tidy feature');
  $this
    ->assertText('Import and Maintain 1300+ Node Items');
  $this
    ->assertText('Documentation update');
  $this
    ->assertText('An HTTP error 404 occured');
  $this
    ->assertText('Does it work with Feeds Image Grabber');
  $this
    ->assertText('Node published date not being correctly mapped (set to 1 Jan 1970)');
  $this
    ->assertText('fields to fill xpath not displayed in importer interface except for "body"');
  $this
    ->assertText('parsing link field');
  $this
    ->assertText('Error when switching to XML Parser');
  $this
    ->assertText('Duplicate content even if "unique target" is set');
  $this
    ->assertText('Labels/field names become meaningless with Data Processor');
  $this
    ->assertText('Xpath namespace help');
  $this
    ->assertText('warning: mysql_real_escape_string()');
  $this
    ->assertText('Feeds XPath Parser: warning: Invalid argument');
  $this
    ->assertText('What am I missing? FeedsXPathParser: No mappings are defined.');
  $this
    ->assertText('CDATA in tag not producing text');
  $this
    ->assertText('Cant map empty fields');
  $this
    ->assertText('Support literal XPath expressions');
  $this
    ->assertText('adding a prefix to a parsed xml value.');
  $this
    ->assertText('Mapping on import');
  $this
    ->assertText('Feeds XPath Parser: HTML parser example for number expressions');
  $this
    ->assertText("I dont want to define any field queries");
  $this
    ->assertText("Document // and other syntax for this module a little better");

  // Test debugging.
  $edit = array(
    'parser[debug][xpathparser:0]' => TRUE,
  );
  $this
    ->postAndCheck($feed_edit_url, $edit, t('Save'), 'XPath Testing XPath HTML Parser has been updated.');
  $this
    ->feedImportItems($fid);
  $this
    ->assertText('<a href="http://drupal.org/node/976478">Xpath Functions</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1048030">Unable to upload .html files</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1050310">Import to multiple content types</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1047788">Parser includes tags in mapped output</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1043608">Errors</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1044546">Loop through HTML - all data is in one node?</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1043728">Patch: add encoding options for PHP tidy feature</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1040132">Import and Maintain 1300+ Node Items</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1043604">Documentation update</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1039492">An HTTP error 404 occured</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1042048">Does it work with Feeds Image Grabber</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/961158">Node published date not being correctly mapped (set to 1 Jan 1970)</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1021474">fields to fill xpath not displayed in importer interface except for "body"</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1040530">parsing link field</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1038912">Error when switching to XML Parser</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1032340">Duplicate content even if "unique target" is set</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/982102">Labels/field names become meaningless with Data Processor</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/1034758">Xpath namespace help</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/908458">warning: mysql_real_escape_string()</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/869076">Feeds XPath Parser: warning: Invalid argument</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/991386">What am I missing? FeedsXPathParser: No mappings are defined.</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/990972">CDATA in tag not producing text</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/989948">Cant map empty fields</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/973324">Support literal XPath expressions</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/958344">adding a prefix to a parsed xml value.</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/914216">Mapping on import</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/863714">Feeds XPath Parser: HTML parser example for number expressions</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/915856">I dont want to define any field queries</a>');
  $this
    ->assertText('<a href="http://drupal.org/node/950150">Document // and other syntax for this module a little better</a>');
  $this
    ->assertText(t('There are no new nodes'));

  // Turn debugging off.
  $edit = array(
    'parser[debug][xpathparser:0]' => FALSE,
  );
  $this
    ->postAndCheck($feed_edit_url, $edit, t('Save'), 'XPath Testing XPath HTML Parser has been updated.');

  // Test that overriding default settings works.
  $edit = array(
    'parser[context]' => '/foo',
    'parser[sources][xpathparser:0]' => 'bar',
    'parser[sources][xpathparser:1]' => 'baz',
  );
  $this
    ->postAndCheck($feed_edit_url, $edit, t('Save'), 'XPath Testing XPath HTML Parser has been updated.');

  // Assert the we don't create an empty node when XPath values don't return anything.
  // That happened at one point.
  $this
    ->feedImportItems($fid);
  $this
    ->assertText(t('There are no new nodes'));

  // Test that validation works.
  $edit = array(
    'parser[context]' => 'sdf asf',
    'parser[sources][xpathparser:0]' => 'asdf[sadfas asdf]',
  );
  $this
    ->drupalPost($feed_edit_url, $edit, 'Save');

  // Check for valid error messages.
  $this
    ->assertText('There was an error with the XPath selector: Invalid expression');
  $this
    ->assertText('There was an error with the XPath selector: Invalid predicate');

  // Make sure the fields are errored out correctly. I.e. we have red outlines.
  $this
    ->assertFieldByXPath('//input[@id="edit-parser-context"][1]/@class', 'form-text required error');
  $this
    ->assertFieldByXPath('//input[@id="edit-parser-sources-xpathparser0"][1]/@class', 'form-text error');

  // Put the values back so we can test inheritance if the form was changed
  // and then changed back.
  $edit = array(
    'parser[context]' => '//tr[starts-with(@class, "odd ") or starts-with(@class, "even ")]',
    'parser[sources][xpathparser:0]' => 'td[1]/a',
    'parser[sources][xpathparser:1]' => 'td[1]/a/@href',
  );
  $this
    ->postAndCheck($feed_edit_url, $edit, t('Save'), t('XPath Testing XPath HTML Parser has been updated.'));

  // Change importer defaults.
  $edit = array(
    'context' => '//tr',
    'sources[xpathparser:0]' => 'booya',
    'sources[xpathparser:1]' => 'boyz',
  );
  $this
    ->postAndCheck($importer_url, $edit, t('Save'), t('Your changes have been saved.'));

  // Make sure the changes propigated.
  $this
    ->drupalGet($feed_edit_url);
  $this
    ->assertFieldByName('parser[context]', '//tr');
  $this
    ->assertFieldByName('parser[sources][xpathparser:0]', 'booya');
  $this
    ->assertFieldByName('parser[sources][xpathparser:1]', 'boyz');

  //Cleanup
  $this
    ->feedDeleteItems($fid);
  $this
    ->assertText(t('Deleted 29 nodes'));
  $this
    ->_testGetRaw($importer_url);
}