Issue #2807889 by chipway, phenaproxima, svendecabooter, quietone: Convert File's Migrate source tests to new base class

8.3.x
Alex Pott 2016-10-23 12:52:17 -07:00
parent 385982db4b
commit 6b9d34c97b
8 changed files with 315 additions and 340 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace Drupal\Tests\file\Kernel\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D6 file source plugin.
*
* @covers \Drupal\file\Plugin\migrate\source\d6\File
*
* @group file
*/
class FileTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['file', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['files'] = [
[
'fid' => 1,
'uid' => 1,
'filename' => 'migrate-test-file-1.pdf',
'filepath' => 'sites/default/files/migrate-test-file-1.pdf',
'filemime' => 'application/pdf',
'filesize' => 890404,
'status' => 1,
'timestamp' => 1382255613,
],
[
'fid' => 2,
'uid' => 1,
'filename' => 'migrate-test-file-2.pdf',
'filepath' => 'sites/default/files/migrate-test-file-2.pdf',
'filemime' => 'application/pdf',
'filesize' => 204124,
'status' => 1,
'timestamp' => 1382255662,
],
];
// The expected results are identical to the source data.
$tests[0]['expected_data'] = $tests[0]['source_data']['files'];
return $tests;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace Drupal\Tests\file\Kernel\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D6 d6_upload_instance source plugin.
*
* @covers \Drupal\file\Plugin\migrate\source\d6\UploadInstance
*
* @group file
*/
class UploadInstanceTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['file', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['node_type'] = [
[
'type' => 'article',
],
[
'type' => 'company',
],
];
$tests[0]['source_data']['variable'] = [
[
'name' => 'upload_article',
'value' => serialize(TRUE),
],
[
'name' => 'upload_company',
'value' => serialize(FALSE),
],
[
'name' => 'upload_uploadsize_default',
'value' => serialize(16),
],
[
'name' => 'upload_extensions_default',
'value' => serialize('txt pdf'),
],
];
// The expected results.
$tests[0]['expected_data'] = [
[
'node_type' => 'article',
'max_filesize' => '16MB',
'file_extensions' => 'txt pdf',
],
];
return $tests;
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace Drupal\Tests\file\Kernel\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D6 d6_upload source plugin.
*
* @covers \Drupal\file\Plugin\migrate\source\d6\Upload
*
* @group file
*/
class UploadTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['file', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['source_data']['upload'] = [
[
'fid' => '1',
'nid' => '1',
'vid' => '1',
'description' => 'file 1-1-1',
'list' => '0',
'weight' => '-1',
],
];
$tests[0]['source_data']['node'] = [
[
'nid' => '1',
'vid' => '1',
'type' => 'story',
'language' => '',
'title' => 'Test title',
'uid' => '1',
'status' => '1',
'created' => '1388271197',
'changed' => '1420861423',
'comment' => '0',
'promote' => '0',
'moderate' => '0',
'sticky' => '0',
'tnid' => '0',
'translate' => '0',
],
];
// The expected results.
$tests[0]['expected_data'] = [
[
'upload' => [
[
'fid' => '1',
'description' => 'file 1-1-1',
'list' => '0',
],
],
'nid' => '1',
'vid' => '1',
'type' => 'story',
],
];
return $tests;
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace Drupal\Tests\file\Kernel\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests D7 file source plugin.
*
* @covers \Drupal\file\Plugin\migrate\source\d7\File
* @group file
*/
class FileTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['file', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
$tests[0]['source_data']['file_managed'] = [
// A public file.
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'public://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
// A private file.
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'private://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
// A temporary file.
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'temporary://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
// A file with a URI scheme that will be filtered out.
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'null://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
];
$tests[0]['source_data']['variable'] = [
[
'name' => 'file_public_path',
'value' => serialize('sites/default/files'),
],
[
'name' => 'file_private_path',
'value' => serialize('/path/to/private/files'),
],
[
'name' => 'file_temporary_path',
'value' => serialize('/tmp'),
],
];
// The expected results will include only the first three files, since we
// are configuring the plugin to filter out the file with the null URI
// scheme.
$tests[0]['expected_data'] = array_slice($tests[0]['source_data']['file_managed'], 0, 3);
// The filepath property will vary by URI scheme.
$tests[0]['expected_data'][0]['filepath'] = 'sites/default/files/cube.jpeg';
$tests[0]['expected_data'][1]['filepath'] = '/path/to/private/files/cube.jpeg';
$tests[0]['expected_data'][2]['filepath'] = '/tmp/cube.jpeg';
// Do an automatic count.
$tests[0]['expected_count'] = NULL;
// Set up plugin configuration.
$tests[0]['configuration'] = [
'constants' => [
'source_base_path' => '/path/to/files',
],
// Only return files which use one of these URI schemes.
'scheme' => ['public', 'private', 'temporary'],
];
return $tests;
}
}

View File

@ -1,54 +0,0 @@
<?php
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
/**
* Tests D6 file source plugin.
*
* @group file
*/
class FileTest extends MigrateSqlSourceTestCase {
const PLUGIN_CLASS = 'Drupal\file\Plugin\migrate\source\d6\File';
protected $migrationConfiguration = array(
'id' => 'test',
'source' => array(
'plugin' => 'd6_file',
),
);
protected $expectedResults = array(
array(
'fid' => 1,
'uid' => 1,
'filename' => 'migrate-test-file-1.pdf',
'filepath' => 'sites/default/files/migrate-test-file-1.pdf',
'filemime' => 'application/pdf',
'filesize' => 890404,
'status' => 1,
'timestamp' => 1382255613,
),
array(
'fid' => 2,
'uid' => 1,
'filename' => 'migrate-test-file-2.pdf',
'filepath' => 'sites/default/files/migrate-test-file-2.pdf',
'filemime' => 'application/pdf',
'filesize' => 204124,
'status' => 1,
'timestamp' => 1382255662,
),
);
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->databaseContents['files'] = $this->expectedResults;
parent::setUp();
}
}

View File

@ -1,65 +0,0 @@
<?php
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
use Drupal\file\Plugin\migrate\source\d6\UploadInstance;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
/**
* Tests d6_upload_instance source plugin.
*
* @group file
*/
class UploadInstanceTest extends MigrateSqlSourceTestCase {
const PLUGIN_CLASS = UploadInstance::class;
protected $migrationConfiguration = array(
'id' => 'test',
'source' => array(
'plugin' => 'd6_upload_instance',
),
);
protected $expectedResults = array(
array(
'node_type' => 'article',
'max_filesize' => '16MB',
'file_extensions' => 'txt pdf',
),
);
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->databaseContents['node_type'] = array(
array(
'type' => 'article',
),
array(
'type' => 'company',
),
);
$this->databaseContents['variable'] = array(
array(
'name' => 'upload_article',
'value' => serialize(TRUE),
),
array(
'name' => 'upload_company',
'value' => serialize(FALSE),
),
array(
'name' => 'upload_uploadsize_default',
'value' => serialize(16),
),
array(
'name' => 'upload_extensions_default',
'value' => serialize('txt pdf'),
),
);
parent::setUp();
}
}

View File

@ -1,75 +0,0 @@
<?php
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d6;
use Drupal\file\Plugin\migrate\source\d6\Upload;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
/**
* Tests d6_upload source plugin.
*
* @group file
*/
class UploadTest extends MigrateSqlSourceTestCase {
const PLUGIN_CLASS = Upload::class;
protected $migrationConfiguration = array(
'id' => 'test',
'source' => array(
'plugin' => 'd6_upload',
),
);
protected $expectedResults = array(
array(
'upload' => array(
array(
'fid' => '1',
'description' => 'file 1-1-1',
'list' => '0',
),
),
'nid' => '1',
'vid' => '1',
'type' => 'story',
),
);
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->databaseContents['upload'] = array(
array(
'fid' => '1',
'nid' => '1',
'vid' => '1',
'description' => 'file 1-1-1',
'list' => '0',
'weight' => '-1',
),
);
$this->databaseContents['node'] = array(
array(
'nid' => '1',
'vid' => '1',
'type' => 'story',
'language' => '',
'title' => 'Test title',
'uid' => '1',
'status' => '1',
'created' => '1388271197',
'changed' => '1420861423',
'comment' => '0',
'promote' => '0',
'moderate' => '0',
'sticky' => '0',
'tnid' => '0',
'translate' => '0',
),
);
parent::setUp();
}
}

View File

@ -1,146 +0,0 @@
<?php
/**
* @file
* Contains \Drupal\Tests\file\Unit\Plugin\migrate\source\d7\FileTest.
*/
namespace Drupal\Tests\file\Unit\Plugin\migrate\source\d7;
use Drupal\Core\Database\Query\ConditionInterface;
use Drupal\file\Plugin\migrate\source\d7\File;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
/**
* Tests D7 file source plugin.
*
* @group file
*/
class FileTest extends MigrateSqlSourceTestCase {
const PLUGIN_CLASS = 'Drupal\Tests\file\Unit\Plugin\migrate\source\d7\TestFile';
protected $migrationConfiguration = array(
'id' => 'test',
'source' => array(
'plugin' => 'd7_file',
'constants' => array(
'source_base_path' => '/path/to/files',
),
// Used by testFilteringByScheme().
'scheme' => array(
'public',
'private',
),
),
'destination' => array(
'plugin' => 'entity:file',
),
);
protected $expectedResults = [
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'public://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
];
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->databaseContents['file_managed'] = $this->expectedResults;
parent::setUp();
}
/**
* Tests that public file URIs are properly transformed by prepareRow().
*/
public function testPublicUri() {
$this->source->publicPath = 'sites/default/files';
$row = new Row(['uri' => 'public://foo.png'], ['uri' => []]);
$this->source->prepareRow($row);
$this->assertEquals('sites/default/files/foo.png',
$row->getSourceProperty('filepath'));
}
/**
* Tests that private file URIs are properly transformed by prepareRow().
*/
public function testPrivateUri() {
$this->source->privatePath = '/path/to/private/files';
$row = new Row(['uri' => 'private://baz.jpeg'], ['uri' => []]);
$this->source->prepareRow($row);
$this->assertEquals('/path/to/private/files/baz.jpeg',
$row->getSourceProperty('filepath'));
}
/**
* Tests that temporary file URIs are property transformed by prepareRow().
*/
public function testTemporaryUri() {
$this->source->temporaryPath = '/tmp';
$row = new Row(['uri' => 'temporary://camelot/lancelot.gif'],
['uri' => []]);
$this->source->prepareRow($row);
$this->assertEquals('/tmp/camelot/lancelot.gif',
$row->getSourceProperty('filepath'));
}
/**
* Tests that it's possible to filter files by scheme.
*/
public function testFilteringByScheme() {
$query_conditions = $this->source->query()->conditions();
$scheme_condition = end($query_conditions);
$this->assertInstanceOf(ConditionInterface::class, $scheme_condition['field']);
$conditions = $scheme_condition['field']->conditions();
$this->assertSame('uri', $conditions[0]['field']);
$this->assertSame('LIKE', $conditions[0]['operator']);
$this->assertSame('public://%', $conditions[0]['value']);
$this->assertSame('uri', $conditions[1]['field']);
$this->assertSame('LIKE', $conditions[1]['operator']);
$this->assertSame('private://%', $conditions[1]['value']);
}
}
/**
* Testing version of \Drupal\file\Plugin\migrate\source\d7\File.
*
* Exposes inaccessible properties.
*/
class TestFile extends File {
/**
* The public file directory path.
*
* @var string
*/
public $publicPath;
/**
* The private file directory path, if any.
*
* @var string
*/
public $privatePath;
/**
* The temporary file directory path.
*
* @var string
*/
public $temporaryPath;
}