View source
<?php
namespace Drupal\feeds_xpathparser\Tests;
use Drupal\feeds_xpathparser\WebTestBase;
class XPathXMLParserTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'XML Parser',
'description' => 'Regression tests for Feeds XPath XML parser.',
'group' => 'Feeds XPath Parser',
);
}
public function test() {
$this
->createImporterConfiguration('XPathXML', 'xpath_xml');
$this
->setPlugin('xpath_xml', 'parser', 'feeds_xpathparser_xml');
$importer_url = self::FEEDS_BASE . '/xpath_xml/settings/parser';
$this
->drupalGet($importer_url);
$this
->assertText('No XPath mappings are defined.');
$this
->addMappings('xpath_xml', array(
0 => array(
'source' => 'xpathparser:0',
'target' => 'title',
'unique' => FALSE,
),
1 => array(
'source' => 'xpathparser:1',
'target' => 'guid',
'unique' => TRUE,
),
2 => array(
'source' => 'xpathparser:2',
'target' => 'body',
),
));
$edit = array(
'context' => '//entry',
'sources[xpathparser:0]' => 'title',
'sources[xpathparser:1]' => 'id',
'sources[xpathparser:2]' => 'id',
'allow_override' => TRUE,
);
$this
->postAndCheck($importer_url, $edit, 'Save', 'Your changes have been saved.');
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_xpathparser') . '/tests/';
$fid = $this
->createFeed('xpath_xml', $path . 'sample_atom_feed.xml', 'XPathXML XPath XML Parser');
$feed_edit_url = "feed/{$fid}/edit";
$this
->assertText('Created 3 nodes');
$this
->feedImportItems($fid);
$this
->assertText('There are no new nodes');
$this
->drupalGet('node');
$this
->assertText('Atom-Powered Robots Run Amok');
$this
->assertText('My dog Jack is the best.');
$this
->assertText('Physics is cool.');
$edit = array(
'parser[debug][xpathparser:0]' => TRUE,
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$this
->feedImportItems($fid);
$this
->assertText('<title>Atom-Powered Robots Run Amok</title>');
$this
->assertText('<title>My dog Jack is the best.</title>');
$this
->assertText('<title>Physics is cool.</title>');
$this
->assertText('There are no new nodes.');
$edit = array(
'parser[debug][xpathparser:0]' => FALSE,
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$this
->setSettings('xpath_xml', 'processor', array(
'update_existing' => 2,
));
$edit = array(
'fetcher[source]' => $path . 'sample_atom_feed_updated.xml',
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$this
->feedImportItems($fid);
$this
->assertText('Updated 1');
$this
->drupalGet('node');
$this
->assertText('Atom-Powered Robots Run Amok');
$this
->assertText('My dog Jack is the best.');
$this
->assertText('Physics is really cool.');
$this
->assertNoText('Physics is cool.');
$this
->drupalGet('node/3');
$this
->assertText('Physics is really cool.');
$this
->setSettings('xpath_xml', 'processor', array(
'update_existing' => 1,
));
$edit = array(
'fetcher[source]' => $path . 'sample_atom_feed.xml',
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$this
->feedImportItems($fid);
$this
->assertText('Updated 1');
$this
->drupalGet('node');
$this
->assertText('Atom-Powered Robots Run Amok');
$this
->assertText('My dog Jack is the best.');
$this
->assertText('Physics is cool.');
$this
->assertNoText('Physics is really cool.');
$this
->drupalGet('node/3');
$this
->assertText('Physics is cool.');
$edit = array(
'parser[context]' => '/foo',
'parser[sources][xpathparser:0]' => 'bar',
'parser[sources][xpathparser:1]' => 'baz',
'parser[sources][xpathparser:2]' => 'wee',
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$this
->feedImportItems($fid);
$this
->assertText('There are no new nodes.');
$edit = array(
'parser[context]' => 'sdf asf',
'parser[sources][xpathparser:0]' => 'asdf[sadfas asdf]',
);
$this
->drupalPost($feed_edit_url, $edit, 'Save');
$this
->assertText('There was an error with the XPath selector: Invalid expression');
$this
->assertText('There was an error with the XPath selector: Invalid predicate');
$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');
$edit = array(
'parser[context]' => '//entry',
'parser[sources][xpathparser:0]' => 'title',
'parser[sources][xpathparser:1]' => 'id',
'parser[sources][xpathparser:2]' => 'id',
);
$this
->postAndCheck($feed_edit_url, $edit, 'Save', 'XPathXML XPath XML Parser has been updated.');
$edit = array(
'context' => '//tr',
'sources[xpathparser:0]' => 'booya',
'sources[xpathparser:1]' => 'boyz',
'sources[xpathparser:2]' => 'woot',
);
$this
->postAndCheck($importer_url, $edit, 'Save', 'Your changes have been saved.');
$this
->drupalGet($feed_edit_url);
$this
->assertFieldByName('parser[context]', '//tr');
$this
->assertFieldByName('parser[sources][xpathparser:0]', 'booya');
$this
->assertFieldByName('parser[sources][xpathparser:1]', 'boyz');
$this
->assertFieldByName('parser[sources][xpathparser:2]', 'woot');
$this
->assertRaw('Field <strong>GUID</strong> is mandatory and considered unique: only one item per GUID value will be created.');
$this
->setSettings('xpath_xml', 'parser', array(
'allow_override' => FALSE,
));
$this
->drupalGet($feed_edit_url);
$this
->assertNoText('XPath Parser Settings');
$this
->assertNoField('context');
}
public function testVariables() {
$this
->createImporterConfiguration();
$this
->setPlugin('syndication', 'parser', 'feeds_xpathparser_xml');
$importer_url = self::FEEDS_BASE . '/syndication/settings/parser';
$this
->addMappings('syndication', array(
0 => array(
'source' => 'xpathparser:0',
'target' => 'title',
'unique' => FALSE,
),
1 => array(
'source' => 'xpathparser:1',
'target' => 'guid',
'unique' => TRUE,
),
2 => array(
'source' => 'xpathparser:2',
'target' => 'body',
),
));
$edit = array(
'context' => '//entry',
'sources[xpathparser:0]' => 'title',
'sources[xpathparser:1]' => 'id',
'sources[xpathparser:2]' => 'link/@$title',
);
$this
->postAndCheck($importer_url, $edit, 'Save', 'Your changes have been saved.');
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_xpathparser') . '/tests/';
$fid = $this
->createFeed('syndication', $path . 'rewrite_test.xml', 'Testing XPath XML Parser');
$feed_edit_url = 'feed/' . $fid . '/edit';
$this
->assertText('Created 3 nodes');
$this
->drupalGet('node');
}
}