View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Utility\Token;
use Drupal\feeds\EntityFinderInterface;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\Exception\TargetValidationException;
use Drupal\feeds\FeedTypeInterface;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
use Drupal\Tests\feeds\Traits\FeedsMockingTrait;
use Drupal\user\Entity\Role;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
abstract class FileTestBase extends FeedsKernelTestBase {
use FeedsMockingTrait;
protected $entityTypeManager;
protected $client;
protected $token;
protected $fileSystem;
protected $targetPlugin;
public function setUp() {
parent::setUp();
$this
->setUpFileFields();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->client = $this
->prophesize(ClientInterface::class);
$this->token = $this
->prophesize(Token::class);
$this->entityFieldManager = $this
->prophesize(EntityFieldManagerInterface::class);
$this->entityFieldManager
->getFieldStorageDefinitions('file')
->willReturn([]);
$this->entityFinder = $this
->prophesize(EntityFinderInterface::class);
$this->entityFinder
->findEntities(Argument::cetera())
->willReturn([]);
$this->fileSystem = $this
->prophesize(FileSystemInterface::class);
$referenceable_entity_type = $this
->prophesize(EntityTypeInterface::class);
$referenceable_entity_type
->getKey('label')
->willReturn('file label');
$this->entityTypeManager
->getDefinition('file')
->willReturn($referenceable_entity_type)
->shouldBeCalled();
$configuration = [
'feed_type' => $this
->createMock(FeedTypeInterface::class),
'target_definition' => $this
->getTargetDefinition(),
];
$this->targetPlugin = $this
->getMockBuilder($this
->getTargetPluginClass())
->setMethods([
'getDestinationDirectory',
])
->setConstructorArgs([
$configuration,
'file',
[],
$this->entityTypeManager
->reveal(),
$this->client
->reveal(),
$this->token
->reveal(),
$this->entityFieldManager
->reveal(),
$this->entityFinder
->reveal(),
$this->fileSystem
->reveal(),
])
->getMock();
$this->targetPlugin
->expects($this
->any())
->method('getDestinationDirectory')
->will($this
->returnValue('public:/'));
$this
->installConfig([
'user',
]);
Role::load(Role::ANONYMOUS_ID)
->grantPermission('access content')
->save();
}
protected abstract function getTargetPluginClass();
protected abstract function getTargetDefinition();
public function testPrepareValue(array $expected, array $values, $expected_exception = NULL, $expected_exception_message = NULL) {
$method = $this
->getProtectedClosure($this->targetPlugin, 'prepareValue');
if (isset($values['target_id'])) {
$file_path = strtr($values['target_id'], [
'[url]' => $this
->resourcesPath(),
]);
$values['target_id'] = strtr($values['target_id'], [
'[url]' => $this
->resourcesUrl(),
]);
if (file_exists($file_path)) {
$this->client
->request('GET', $values['target_id'], Argument::any())
->will(function () use ($file_path) {
return new Response(200, [], file_get_contents($file_path));
});
}
else {
$this->client
->request('GET', $values['target_id'], Argument::any())
->will(function () {
return new Response(404, [], '');
});
}
}
if ($expected_exception) {
$this
->expectException($expected_exception);
if ($expected_exception_message) {
$expected_exception_message = strtr($expected_exception_message, [
'[url]' => $this
->resourcesUrl(),
]);
$this
->expectExceptionMessage($expected_exception_message);
}
}
$method(0, $values);
foreach ($expected as $key => $value) {
$this
->assertEquals($value, $values[$key]);
}
}
public function dataProviderPrepareValue() {
$return = [
'empty-file' => [
'expected' => [],
'values' => [
'target_id' => '',
],
'expected_exception' => EmptyFeedException::class,
'expected_exception_message' => 'The given file url is empty.',
],
'file-success' => [
'expected' => [
'target_id' => 1,
],
'values' => [
'target_id' => '[url]/assets/attersee.jpeg',
],
],
'file-uppercase' => [
'expected' => [
'target_id' => 1,
],
'values' => [
'target_id' => '[url]/assets/attersee.JPG',
],
],
'file-not-found' => [
'expected' => [],
'values' => [
'target_id' => '[url]/assets/not-found.jpg',
],
'expected_exception' => TargetValidationException::class,
'expected_exception_message' => 'Download of <em class="placeholder">[url]/assets/not-found.jpg</em> failed with code 404.',
],
'invalid-extension' => [
'expected' => [],
'values' => [
'target_id' => '[url]/file.foo',
],
'expected_exception' => TargetValidationException::class,
'expected_exception_message' => 'The file, <em class="placeholder">[url]/file.foo</em>, failed to save because the extension, <em class="placeholder">foo</em>, is invalid.',
],
];
return $return;
}
}