From 6b9d34c97bfc067bf3140cdcd5b472ad645f9aa1 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 23 Oct 2016 12:52:17 -0700 Subject: [PATCH] Issue #2807889 by chipway, phenaproxima, svendecabooter, quietone: Convert File's Migrate source tests to new base class --- .../Plugin/migrate/source/d6/FileTest.php | 57 +++++++ .../migrate/source/d6/UploadInstanceTest.php | 68 ++++++++ .../Plugin/migrate/source/d6/UploadTest.php | 78 ++++++++++ .../Plugin/migrate/source/d7/FileTest.php | 112 ++++++++++++++ .../Plugin/migrate/source/d6/FileTest.php | 54 ------- .../migrate/source/d6/UploadInstanceTest.php | 65 -------- .../Plugin/migrate/source/d6/UploadTest.php | 75 --------- .../Plugin/migrate/source/d7/FileTest.php | 146 ------------------ 8 files changed, 315 insertions(+), 340 deletions(-) create mode 100644 core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php create mode 100644 core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadInstanceTest.php create mode 100644 core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadTest.php create mode 100644 core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php delete mode 100644 core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/FileTest.php delete mode 100644 core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadInstanceTest.php delete mode 100644 core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadTest.php delete mode 100644 core/modules/file/tests/src/Unit/Plugin/migrate/source/d7/FileTest.php diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php new file mode 100644 index 00000000000..cc8c9b36faf --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php @@ -0,0 +1,57 @@ + 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; + } + +} diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadInstanceTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadInstanceTest.php new file mode 100644 index 00000000000..711b36fec32 --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadInstanceTest.php @@ -0,0 +1,68 @@ + '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; + } + +} diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadTest.php new file mode 100644 index 00000000000..96389d5dd22 --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/UploadTest.php @@ -0,0 +1,78 @@ + '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; + } + +} diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php new file mode 100644 index 00000000000..8ae0ec41fa1 --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php @@ -0,0 +1,112 @@ + '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; + } + +} diff --git a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/FileTest.php b/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/FileTest.php deleted file mode 100644 index 55437e23dc9..00000000000 --- a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/FileTest.php +++ /dev/null @@ -1,54 +0,0 @@ - '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(); - } - -} diff --git a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadInstanceTest.php b/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadInstanceTest.php deleted file mode 100644 index 6232680026a..00000000000 --- a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadInstanceTest.php +++ /dev/null @@ -1,65 +0,0 @@ - '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(); - } - -} diff --git a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadTest.php b/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadTest.php deleted file mode 100644 index 026cc35dc94..00000000000 --- a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d6/UploadTest.php +++ /dev/null @@ -1,75 +0,0 @@ - '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(); - } - -} diff --git a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d7/FileTest.php b/core/modules/file/tests/src/Unit/Plugin/migrate/source/d7/FileTest.php deleted file mode 100644 index 6a97e739893..00000000000 --- a/core/modules/file/tests/src/Unit/Plugin/migrate/source/d7/FileTest.php +++ /dev/null @@ -1,146 +0,0 @@ - '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; - -}