You are here

class FeedsJSONPathParserTestCase in Feeds JSONPath Parser 7

Same name and namespace in other branches
  1. 6 tests/feeds_jsonpath_parser.test \FeedsJSONPathParserTestCase

Test single feeds.

Hierarchy

Expanded class hierarchy of FeedsJSONPathParserTestCase

File

tests/feeds_jsonpath_parser.test, line 11
Tests for FeedsJSONPathParser.inc.

View source
class FeedsJSONPathParserTestCase extends FeedsWebTestCase {

  /**
   * Describe this test.
   */
  public static function getInfo() {
    return array(
      'name' => t('JSONPath Parser'),
      'description' => t('Regression tests for Feeds JSONPath parser.'),
      'group' => t('Feeds JSONPath Parser'),
    );
  }

  /**
   * Set up test.
   */
  public function setUp(array $modules = array()) {
    $modules[] = 'libraries';
    $modules[] = 'feeds_jsonpath_parser';
    parent::setUp($modules);
  }

  /**
   * Run tests.
   */
  public function test() {
    $this
      ->createImporterConfiguration('JSONPath', 'jsonpath');
    $this
      ->setPlugin('jsonpath', 'FeedsJSONPathParser');
    $this
      ->addMappings('jsonpath', array(
      array(
        'source' => 'jsonpath_parser:0',
        'target' => 'title',
        'unique' => FALSE,
      ),
      array(
        'source' => 'jsonpath_parser:1',
        'target' => 'url',
        'unique' => TRUE,
      ),
    ));

    // Set importer default settings.
    $edit = array(
      'jsonpath[context]' => '$.store.book[*]',
      'jsonpath[sources][jsonpath_parser:0]' => 'author',
      'jsonpath[sources][jsonpath_parser:1]' => 'price',
      'jsonpath[allow_override]' => TRUE,
    );
    $edit_url = 'admin/structure/feeds/jsonpath/settings/FeedsJSONPathParser';
    $node_save_text = 'Basic page Testing JSONPath Parser has been updated.';
    $this
      ->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.');

    // Test import.
    $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_jsonpath_parser') . '/tests/feeds_jsonpath_parser';
    $nid = $this
      ->createFeedNode('jsonpath', $path . '/test.json', 'Testing JSONPath Parser');
    $this
      ->assertText('Created 4 nodes');

    // Import again, this verifies url field was mapped correctly.
    $this
      ->drupalPost('node/' . $nid . '/import', array(), 'Import');
    $this
      ->assertText('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('Nigel Rees');
    $this
      ->assertText('Evelyn Waugh');
    $this
      ->assertText('Herman Melville');
    $this
      ->assertText('J. R. R. Tolkien');

    // Test that overriding default settings works.
    $edit = array(
      'feeds[FeedsJSONPathParser][jsonpath][context]' => '$.foo',
      'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'bar',
      'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'baz',
    );
    $this
      ->postAndCheck('node/' . $nid . '/edit', $edit, 'Save', $node_save_text);

    // Assert the we don't create an empty node when JSONPath values don't
    // return anything. That happened at one point.
    $this
      ->drupalPost('node/' . $nid . '/import', array(), 'Import');
    $this
      ->assertText('There are no new nodes');

    // Put the values back so we can test inheritance if the form was changed
    // and then changed back.
    $edit = array(
      'feeds[FeedsJSONPathParser][jsonpath][context]' => '$.store.book[*]',
      'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]' => 'author',
      'feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]' => 'price',
    );
    $this
      ->postAndCheck('node/' . $nid . '/edit', $edit, 'Save', $node_save_text);

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

    // Make sure the changes propigated.
    $this
      ->drupalGet('node/' . $nid . '/edit');
    $this
      ->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][context]', '//tr');
    $this
      ->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:0]', 'booya');
    $this
      ->assertFieldByName('feeds[FeedsJSONPathParser][jsonpath][sources][jsonpath_parser:1]', 'boyz');

    // Turn off allow_override.
    $edit = array(
      'jsonpath[allow_override]' => FALSE,
    );
    $this
      ->postAndCheck($edit_url, $edit, 'Save', 'Your changes have been saved.');
    $this
      ->drupalGet('node/' . $nid . '/edit');
    $this
      ->assertNoText('JSONPath Parser Settings');

    // Test byte replacement.
    $this
      ->assertEqual('ࡇ abc ʦ 𡃁', FeedsJSONPathParser::convertFourBytes('ࡇ abc ʦ 𡃁'));
    $this
      ->assertEqual('ࡇ abc ʦ ', FeedsJSONPathParser::stripFourBytes('ࡇ abc ʦ 𡃁'));
  }

  /**
   * Submit a form and confirm the expected fields are present.
   */
  public function postAndCheck($url, $edit, $button, $saved_text) {

    // Request the page and confirm it loads correctly.
    $this
      ->drupalGet($url);
    $this
      ->assertResponse(200);

    // Submit data to the form.
    $this
      ->drupalPost(NULL, $edit, $button);

    // Confirm the expected string was found on the form.
    $this
      ->assertText($saved_text);

    // Reload the form.
    $this
      ->drupalGet($url);

    // Confirm each of the fields is present on the form and has the expected
    // values.
    foreach ($edit as $key => $value) {
      $this
        ->assertFieldByName($key, $value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsJSONPathParserTestCase::getInfo public static function Describe this test.
FeedsJSONPathParserTestCase::postAndCheck public function Submit a form and confirm the expected fields are present.
FeedsJSONPathParserTestCase::setUp public function Set up test.
FeedsJSONPathParserTestCase::test public function Run tests.