View source
<?php
namespace Drupal\Tests\feeds_ex\Functional\Feeds\Parser;
class JmesPathParserTest extends ParserTestBase {
use ContextTestTrait;
protected $parserId = 'jmespath';
public function dataProviderValidContext() {
return [
[
'items',
],
[
'length(people)',
],
[
'sort_by(people, &age)',
],
];
}
public function dataProviderInvalidContext() {
return [
[
'!! ',
'Syntax error at character',
],
];
}
public function testInvalidMappingSource() {
$this
->drupalGet('/admin/structure/feeds/manage/' . $this->feedType
->id() . '/mapping');
$edit = [
'context' => '@',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$edit = [
'mappings[1][map][value][select]' => '__new',
'mappings[1][map][value][__new][value]' => 'items[].join(`__`,[title,description)',
'mappings[1][map][value][__new][machine_name]' => 'title_desc',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->pageTextContains('Syntax error at character');
$this->feedType = $this
->reloadEntity($this->feedType);
$this
->assertEquals([], $this->feedType
->getParser()
->getConfiguration('sources'));
}
public function testImportWithInvalidExpression() {
node_add_body_field($this->nodeType);
$feed_type = $this
->createFeedType([
'parser' => 'jmespath',
'parser_configuration' => [
'context' => [
'value' => '@',
],
'sources' => [
'title' => [
'label' => 'Title',
'value' => 'items[].title',
],
'title_desc' => [
'label' => 'Title and description',
'value' => 'items[].join(`__`,[title,description)',
],
],
],
'custom_sources' => [
'title' => [
'label' => 'Title',
'value' => 'items[].title',
'machine_name' => 'title',
],
'title_desc' => [
'label' => 'Title and description',
'value' => 'items[].join(`__`,[title,description)',
'machine_name' => 'title_desc',
],
],
'mappings' => [
[
'target' => 'title',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'body',
'map' => [
'value' => 'title_desc',
],
'settings' => [
'format' => 'plain_text',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesUrl() . '/test.json',
]);
$this
->batchImport($feed);
$this
->assertSession()
->pageTextContains('There are no new Article items.');
$this
->assertSession()
->pageTextContains('Syntax error at character');
}
}