Skip to content

Commit cbc6139

Browse files
authored
ImportCoordinates tested and refactored (#402)
* ImportCoordinates tested and refactored
1 parent 5a286b9 commit cbc6139

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

includes/ContentImport/ImportCoordinates.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,42 +80,21 @@ public function validate() {
8080
* @return string|null The import slug if set or `null` if not set.
8181
*/
8282
public function get_importer_for( $importer_type ) {
83-
return isset( $this->importers[ $importer_type ] )
84-
? $this->importers[ $importer_type ]
85-
: null;
83+
return $this->importers[ $importer_type ] ?? null;
8684
}
8785

8886
/**
8987
* Parses the importers from request superglobals.
9088
*/
9189
public function parse_importers_from_request(): void {
92-
// bitmask
93-
$source = isset( $_REQUEST[ self::IMPORTERS_GLOBAL_KEY ] ) * 100
94-
+ isset( $_POST[ self::IMPORTERS_GLOBAL_KEY ] ) * 10
95-
+ isset( $_GET[ self::IMPORTERS_GLOBAL_KEY ] );
96-
97-
switch ( $source ) {
98-
case 100:
99-
case 101;
100-
case 111:
101-
case 110:
102-
$source = $_REQUEST;
103-
break;
104-
case 010:
105-
case 011:
106-
case 000:
107-
default:
108-
$source = $_POST;
109-
break;
110-
case 001:
111-
$source = $_GET;
90+
$importers = array();
91+
foreach ( array( INPUT_POST, INPUT_GET ) as $input_type ) {
92+
if ( filter_has_var( $input_type, self::IMPORTERS_GLOBAL_KEY ) ) {
93+
$importers = filter_input( $input_type, self::IMPORTERS_GLOBAL_KEY, FILTER_FORCE_ARRAY );
11294
break;
95+
}
11396
}
11497

115-
$importers = isset( $source[ self::IMPORTERS_GLOBAL_KEY ] ) && is_array( $source[ self::IMPORTERS_GLOBAL_KEY ] )
116-
? $source[ self::IMPORTERS_GLOBAL_KEY ]
117-
: array();
118-
11998
foreach ( $importers as $importer_type => $slug ) {
12099
$this->set_importer_for( $importer_type, $slug );
121100
}

tests/phpunit/ContentImport/TestImportCoordinates.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setUp(): void {
2323
$this->test->dest_lang = 'it_IT';
2424
}
2525

26-
public static function provider_validate(): array {
26+
public static function providerValidate(): array {
2727
$post = \Mockery::mock( \WP_Post::class );
2828
return array(
2929
array( null, null, null, null, null, false ),
@@ -36,14 +36,41 @@ public static function provider_validate(): array {
3636
}
3737

3838
/**
39-
* @dataProvider provider_validate
39+
* @dataProvider providerValidate
4040
*/
41-
public function test_validate( $post_a, $post_b, $source_post, $lang_a, $lang_b, $expected ): void {
41+
public function testValidate( $post_a, $post_b, $source_post, $lang_a, $lang_b, $expected ): void {
4242
Functions\expect( 'get_blog_post' )->andReturn( $post_a, $post_b );
4343
Functions\expect( 'get_blog_option' )->andReturn( $lang_a, $lang_b );
4444

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

4747
$this->assertEquals( $expected, $this->test->validate() );
4848
}
49+
50+
public function testParseImportersFromPost(): void {
51+
Functions\expect( 'filter_has_var' )
52+
->once()
53+
->with( INPUT_POST, ImportCoordinates::IMPORTERS_GLOBAL_KEY )
54+
->andReturn( false );
55+
Functions\expect( 'filter_has_var' )
56+
->once()
57+
->with( INPUT_GET, ImportCoordinates::IMPORTERS_GLOBAL_KEY )
58+
->andReturn( true );
59+
Functions\expect( 'filter_input' )
60+
->once()
61+
->with( INPUT_GET, ImportCoordinates::IMPORTERS_GLOBAL_KEY, FILTER_FORCE_ARRAY )
62+
->andReturn( array( 'pagesType' => 'pagesSlug' ) );
63+
64+
$this->assertNull( $this->test->get_importer_for( 'pagesType' ) );
65+
66+
$this->test->parse_importers_from_request();
67+
68+
$this->assertEquals( 'pagesSlug', $this->test->get_importer_for( 'pagesType' ) );
69+
}
70+
71+
public function testSetImporterFor(): void {
72+
$this->test->set_importer_for( 'postsType', 'postsSlug' );
73+
74+
$this->assertEquals( 'postsSlug', $this->test->get_importer_for( 'postsType' ) );
75+
}
4976
}

0 commit comments

Comments
 (0)