Skip to content

Commit

Permalink
ImportCoordinates tested and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Nov 16, 2024
1 parent 5a286b9 commit c5d76f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
34 changes: 7 additions & 27 deletions includes/ContentImport/ImportCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace lloc\Msls\ContentImport;

use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsRequest;

class ImportCoordinates {

Expand Down Expand Up @@ -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 );
}
Expand Down
34 changes: 31 additions & 3 deletions tests/phpunit/ContentImport/TestImportCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 ),
Expand All @@ -36,14 +37,41 @@ 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 );

$this->test->source_post = $source_post;

$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' ) );
}
}

0 comments on commit c5d76f3

Please sign in to comment.