View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
use Drupal\Tests\pathauto\Functional\PathautoTestHelperTrait;
class PathTest extends FeedsKernelTestBase {
use PathautoTestHelperTrait;
protected $feedType;
public static $modules = [
'system',
'field',
'node',
'feeds',
'path',
'path_alias',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('path_alias');
$this
->installEntitySchema('node');
$this->feedType = $this
->createFeedTypeForCsv([
'title' => 'title',
'alias' => 'alias',
], [
'processor_configuration' => [
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'authorize' => FALSE,
'values' => [
'type' => 'article',
],
],
'mappings' => [
[
'target' => 'title',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'path',
'map' => [
'alias' => 'alias',
],
],
],
]);
$this->feedType
->save();
}
public function testImportPaths() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$expected = [
1 => '/lorem-ipsum-dolor',
2 => '/ut-wisi-enim',
];
foreach ($expected as $nid => $value) {
$node = Node::load($nid);
$this
->assertEquals($value, $node->path->alias);
}
}
public function testUpdateNodePaths() {
$node = Node::create([
'title' => 'Lorem ipsum',
'type' => 'article',
'path' => [
'alias' => 'lorie',
'pathauto' => 0,
],
]);
$node
->save();
$this
->assertEquals('lorie', $node->path->alias);
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$node = $this
->reloadEntity($node);
$this
->assertEquals('/lorem-ipsum-dolor', $node->path->alias);
}
public function testImportPathsWithPathauto() {
$this
->installPathauto();
$this
->testImportPaths();
}
public function testUpdateNodePathsWithPathauto() {
$this
->installPathauto();
$this
->testUpdateNodePaths();
}
public function testImportPathauto() {
$this
->installPathauto();
$feed_type = $this
->createFeedTypeForCsv([
'title' => 'title',
'alias' => 'alias',
'epsilon' => 'epsilon',
]);
$feed_type
->addMapping([
'target' => 'path',
'map' => [
'alias' => 'alias',
'pathauto' => 'epsilon',
],
]);
$feed_type
->save();
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$expected = [
1 => '/content/lorem-ipsum',
2 => '/ut-wisi-enim',
];
foreach ($expected as $nid => $value) {
$node = Node::load($nid);
$this
->assertEquals($value, $node->path->alias);
}
}
protected function installPathauto() {
$this
->installModule('ctools');
$this
->installModule('token');
$this
->installModule('pathauto');
$this
->installConfig([
'pathauto',
'system',
'node',
]);
$this
->createPattern('node', '/content/[node:title]');
\Drupal::service('router.builder')
->rebuild();
}
}