View source
<?php
namespace Drupal\Tests\feeds\Functional;
use Drupal\filter\Entity\FilterFormat;
class FieldValidationTest extends FeedsBrowserTestBase {
public static $modules = [
'feeds',
'node',
'user',
'file',
'filter',
];
public function testTextFieldValidation() {
$this
->createFieldWithStorage('field_alpha', [
'storage' => [
'settings' => [
'max_length' => 5,
],
],
]);
$feed_type = $this
->createFeedType([
'parser' => 'csv',
'custom_sources' => [
'guid' => [
'label' => 'guid',
'value' => 'guid',
'machine_name' => 'guid',
],
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
'alpha' => [
'label' => 'alpha',
'value' => 'alpha',
'machine_name' => 'alpha',
],
],
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_alpha',
'map' => [
'value' => 'alpha',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesUrl() . '/csv/content.csv',
]);
$this
->batchImport($feed);
$this
->assertText('Created 1 Article.');
$this
->assertText('Failed importing 1 Article.');
$this
->assertText("The content Ut wisi enim ad minim veniam failed to validate with the following errors");
$this
->assertText('field_alpha.0.value: field_alpha label: the text may not be longer than 5 characters.');
}
public function testImportFieldWithAdminFilterFormatInUi() {
node_add_body_field($this->nodeType);
$format = FilterFormat::create([
'format' => 'empty_format',
'name' => 'Empty format',
]);
$format
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer feeds',
'administer filters',
'access site reports',
$format
->getPermissionName(),
]);
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'body' => 'body',
], [
'id' => 'my_feed_type',
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'body',
'map' => [
'value' => 'body',
],
'settings' => [
'format' => $format
->id(),
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
'uid' => $this->adminUser
->id(),
]);
$account = $this
->drupalCreateUser([
'view my_feed_type feeds',
'import my_feed_type feeds',
]);
$this
->drupalLogin($account);
$this
->drupalPostForm('feed/1/import', [], 'Import');
$this
->assertNodeCount(2);
$this
->assertSession()
->pageTextContains('Created 2 Article items');
}
public function testImportFieldWithAdminFilterFormatOnCron() {
node_add_body_field($this->nodeType);
$format = FilterFormat::create([
'format' => 'empty_format',
'name' => 'Empty format',
]);
$format
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer feeds',
'administer filters',
'access site reports',
$format
->getPermissionName(),
]);
$this
->drupalLogin($this->adminUser);
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'body' => 'body',
], [
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'body',
'map' => [
'value' => 'body',
],
'settings' => [
'format' => $format
->id(),
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
'uid' => $this->adminUser
->id(),
]);
$feed
->startCronImport();
$this
->cronRun();
$this
->assertNodeCount(2);
}
}