From c5d76f3531df26f290828c360826979ef151b490 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sat, 16 Nov 2024 15:31:41 +0100 Subject: [PATCH] ImportCoordinates tested and refactored --- includes/ContentImport/ImportCoordinates.php | 34 ++++--------------- .../ContentImport/TestImportCoordinates.php | 34 +++++++++++++++++-- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/includes/ContentImport/ImportCoordinates.php b/includes/ContentImport/ImportCoordinates.php index dbd704b9..2506b8ac 100644 --- a/includes/ContentImport/ImportCoordinates.php +++ b/includes/ContentImport/ImportCoordinates.php @@ -3,6 +3,7 @@ namespace lloc\Msls\ContentImport; use lloc\Msls\MslsBlogCollection; +use lloc\Msls\MslsRequest; class ImportCoordinates { @@ -80,42 +81,21 @@ public function validate() { * @return string|null The import slug if set or `null` if not set. */ public function get_importer_for( $importer_type ) { - return isset( $this->importers[ $importer_type ] ) - ? $this->importers[ $importer_type ] - : null; + return $this->importers[ $importer_type ] ?? null; } /** * Parses the importers from request superglobals. */ public function parse_importers_from_request(): void { - // bitmask - $source = isset( $_REQUEST[ self::IMPORTERS_GLOBAL_KEY ] ) * 100 - + isset( $_POST[ self::IMPORTERS_GLOBAL_KEY ] ) * 10 - + isset( $_GET[ self::IMPORTERS_GLOBAL_KEY ] ); - - switch ( $source ) { - case 100: - case 101; - case 111: - case 110: - $source = $_REQUEST; - break; - case 010: - case 011: - case 000: - default: - $source = $_POST; - break; - case 001: - $source = $_GET; + $importers = array(); + foreach ( array( INPUT_POST, INPUT_GET ) as $input_type ) { + if ( filter_has_var( $input_type, self::IMPORTERS_GLOBAL_KEY ) ) { + $importers = filter_input( $input_type, self::IMPORTERS_GLOBAL_KEY, FILTER_FORCE_ARRAY ); break; + } } - $importers = isset( $source[ self::IMPORTERS_GLOBAL_KEY ] ) && is_array( $source[ self::IMPORTERS_GLOBAL_KEY ] ) - ? $source[ self::IMPORTERS_GLOBAL_KEY ] - : array(); - foreach ( $importers as $importer_type => $slug ) { $this->set_importer_for( $importer_type, $slug ); } diff --git a/tests/phpunit/ContentImport/TestImportCoordinates.php b/tests/phpunit/ContentImport/TestImportCoordinates.php index cd71a53b..e8d57458 100644 --- a/tests/phpunit/ContentImport/TestImportCoordinates.php +++ b/tests/phpunit/ContentImport/TestImportCoordinates.php @@ -3,6 +3,7 @@ namespace lloc\MslsTests\ContentImport; use lloc\Msls\ContentImport\ImportCoordinates; +use lloc\Msls\MslsFields; use lloc\MslsTests\MslsUnitTestCase; use Brain\Monkey\Functions; @@ -23,7 +24,7 @@ public function setUp(): void { $this->test->dest_lang = 'it_IT'; } - public static function provider_validate(): array { + public static function providerValidate(): array { $post = \Mockery::mock( \WP_Post::class ); return array( array( null, null, null, null, null, false ), @@ -36,9 +37,9 @@ public static function provider_validate(): array { } /** - * @dataProvider provider_validate + * @dataProvider providerValidate */ - public function test_validate( $post_a, $post_b, $source_post, $lang_a, $lang_b, $expected ): void { + public function testValidate( $post_a, $post_b, $source_post, $lang_a, $lang_b, $expected ): void { Functions\expect( 'get_blog_post' )->andReturn( $post_a, $post_b ); Functions\expect( 'get_blog_option' )->andReturn( $lang_a, $lang_b ); @@ -46,4 +47,31 @@ public function test_validate( $post_a, $post_b, $source_post, $lang_a, $lang_b, $this->assertEquals( $expected, $this->test->validate() ); } + + public function testParseImportersFromPost(): void { + Functions\expect( 'filter_has_var' ) + ->once() + ->with( INPUT_POST, ImportCoordinates::IMPORTERS_GLOBAL_KEY ) + ->andReturn( false ); + Functions\expect( 'filter_has_var' ) + ->once() + ->with( INPUT_GET, ImportCoordinates::IMPORTERS_GLOBAL_KEY ) + ->andReturn( true ); + Functions\expect( 'filter_input' ) + ->once() + ->with( INPUT_GET, ImportCoordinates::IMPORTERS_GLOBAL_KEY, FILTER_FORCE_ARRAY ) + ->andReturn( array( 'pagesType' => 'pagesSlug' ) ); + + $this->assertNull( $this->test->get_importer_for( 'pagesType' ) ); + + $this->test->parse_importers_from_request(); + + $this->assertEquals( 'pagesSlug', $this->test->get_importer_for( 'pagesType' ) ); + } + + public function testSetImporterFor(): void { + $this->test->set_importer_for( 'postsType', 'postsSlug' ); + + $this->assertEquals( 'postsSlug', $this->test->get_importer_for( 'postsType' ) ); + } }