Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions includes/ContentImport/Importers/ImportersBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ public function selected() {
* @return array<string, \stdClass>
*/
protected function importers_info(): array {
return array_combine(
array_keys( $this->importers_map ),
array_map(
function ( $importer_class ) {
return $importer_class::info();
},
$this->importers_map
)
return array_map(
function ( $importer_class ) {
return $importer_class::info();
},
$this->importers_map
);
}
}
10 changes: 3 additions & 7 deletions includes/ContentImport/Importers/Terms/ShallowDuplicating.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,13 @@ public function import( array $data ) {
return $data;
}

$source_terms_ids = wp_list_pluck( $source_terms, 'term_id' );
$msls_terms = array_combine(
$source_terms_ids,
array_map( array( Term::class, 'create' ), $source_terms_ids )
);
$msls_terms = array_map( array( Term::class, 'create' ), wp_list_pluck( $source_terms, 'term_id' ) );

switch_to_blog( $this->import_coordinates->dest_blog_id );

foreach ( $source_terms as $term ) {
foreach ( $source_terms as $key => $term ) {
// is there a translation for the term in this blog?
$msls_term = $msls_terms[ $term->term_id ];
$msls_term = $msls_terms[ $key ];
$dest_term_id = $msls_term->{$dest_lang};

if ( null === $dest_term_id ) {
Expand Down
22 changes: 11 additions & 11 deletions tests/phpunit/Admin/TestAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function test_settings_field_renders( string $method, string $expected ):
}


function test_rewrite_tizio(): void {
public function test_rewrite_tizio(): void {
$obj = $this->AdminFactory();

$post_type = \Mockery::mock( \WP_Post_Type::class );
Expand All @@ -246,7 +246,7 @@ function test_rewrite_tizio(): void {
$obj->rewrite_tizio( 'tizio' );
}

function test_rewrite_pinko(): void {
public function test_rewrite_pinko(): void {
$obj = $this->AdminFactory();

$post_type = \Mockery::mock( \WP_Post_Type::class );
Expand All @@ -260,7 +260,7 @@ function test_rewrite_pinko(): void {
$obj->rewrite_pinko( 'pinko' );
}

function test_rewrite_pallino(): void {
public function test_rewrite_pallino(): void {
$obj = $this->AdminFactory();

$post_type = \Mockery::mock( \WP_Post_Type::class );
Expand All @@ -274,14 +274,14 @@ function test_rewrite_pallino(): void {
$obj->rewrite_pallino( 'pallino' );
}

function test_content_priority(): void {
public function test_content_priority(): void {
$obj = $this->AdminFactory();

$this->expectOutputRegex( '/^<select id="content_priority" name="msls\[content_priority\]">.*$/' );
$obj->content_priority();
}

function test_validate(): void {
public function test_validate(): void {
$obj = $this->AdminFactory();

$arr = array();
Expand All @@ -299,7 +299,7 @@ function test_validate(): void {
);
}

function test_set_blog_language(): void {
public function test_set_blog_language(): void {
$obj = $this->AdminFactory();

$arr = array(
Expand All @@ -309,7 +309,7 @@ function test_set_blog_language(): void {
$this->assertEquals( array( 'abc' => true ), $obj->set_blog_language( $arr ) );
}

function test_render(): void {
public function test_render(): void {
$obj = $this->AdminFactory();

Functions\expect( 'settings_fields' )->once();
Expand All @@ -321,31 +321,31 @@ function test_render(): void {
$obj->render();
}

function test_language_section(): void {
public function test_language_section(): void {
$obj = $this->AdminFactory();

Functions\when( 'add_settings_field' )->returnArg();

$this->assertEquals( 1, $obj->language_section() );
}

function test_main_section(): void {
public function test_main_section(): void {
$obj = $this->AdminFactory();

Functions\expect( 'add_settings_field' )->times( 12 )->andReturnFirstArg();

$this->assertEquals( 12, $obj->main_section() );
}

function test_advanced_section(): void {
public function test_advanced_section(): void {
$obj = $this->AdminFactory();

Functions\expect( 'add_settings_field' )->times( 6 )->andReturnFirstArg();

$this->assertEquals( 6, $obj->advanced_section() );
}

function test_rewrites_section(): void {
public function test_rewrites_section(): void {
$obj = $this->AdminFactory();

foreach ( array(
Expand Down
101 changes: 26 additions & 75 deletions tests/phpunit/Admin/TestIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,42 @@ public static function get_post_type(): array {
);
}

public function test_get_a_not_empty_post(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[0];

Functions\expect( 'get_edit_post_link' )->once()->andReturn( $edit_link );

$post = $this->get_post( $post_type, 2 );
$obj = $this->IconFactory( $post );

$value = '<a title="Edit the translation in the de_DE-blog" href="' . $edit_link . '"><span class="dashicons dashicons-edit"></span></a>&nbsp;';
$this->assertEquals( $value, $obj->get_a() );
$this->assertEquals( $value, $obj->__toString() );
/**
* Columns: index in get_post_type(), add_query_arg() calls. Only pages get a post_type query arg.
*
* @return array<string, array{int, int}>
*/
public static function post_type_provider(): array {
return array(
'post' => array( 0, 0 ),
'page' => array( 1, 2 ),
);
}

public function test_get_a_not_empty_page(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[1];
#[DataProvider( 'post_type_provider' )]
public function test_get_a_not_empty( int $index, int $add_query_arg_calls ): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[ $index ];

Functions\expect( 'add_query_arg' )->twice()->andReturn( $edit_link );
Functions\expect( 'add_query_arg' )->times( $add_query_arg_calls )->andReturn( $edit_link );
Functions\expect( 'get_edit_post_link' )->once()->andReturn( $edit_link );

$post = $this->get_post( $post_type, 2 );
$obj = $this->IconFactory( $post );
$obj = $this->IconFactory( $this->get_post( $post_type, 2 ) );

$value = '<a title="Edit the translation in the de_DE-blog" href="' . $edit_link . '"><span class="dashicons dashicons-edit"></span></a>&nbsp;';
$this->assertEquals( $value, $obj->get_a() );
$this->assertEquals( $value, $obj->__toString() );
}

public function test_get_a_empty_post(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[0];
#[DataProvider( 'post_type_provider' )]
public function test_get_a_empty( int $index, int $add_query_arg_calls ): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[ $index ];

Functions\expect( 'get_current_blog_id' )->twice()->andReturn( 1 );
Functions\expect( 'get_admin_url' )->twice()->andReturn( $create_link );
Functions\expect( 'add_query_arg' )->times( $add_query_arg_calls )->andReturn( $create_link );
Functions\expect( 'get_edit_post_link' )->twice()->andReturn( null );

$post = $this->get_post( $post_type );
$obj = $this->IconFactory( $post );
$obj = $this->IconFactory( $this->get_post( $post_type ) );

$this->assertInstanceOf( Icon::class, $obj->set_href( 0 ) );

Expand All @@ -102,55 +102,19 @@ public function test_get_a_empty_post(): void {
$this->assertEquals( $value, $obj->__toString() );
}

public function test_get_a_empty_page(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[1];
#[DataProvider( 'post_type_provider' )]
public function test_get_img( int $index, int $add_query_arg_calls ): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[ $index ];

Functions\expect( 'get_current_blog_id' )->twice()->andReturn( 1 );
Functions\expect( 'get_admin_url' )->twice()->andReturn( $create_link );
Functions\expect( 'add_query_arg' )->twice()->andReturn( $create_link );
Functions\expect( 'get_edit_post_link' )->twice()->andReturn( null );

$post = $this->get_post( $post_type );
$obj = $this->IconFactory( $post );

$this->assertInstanceOf( Icon::class, $obj->set_href( 0 ) );

$value = sprintf(
'<a title="Create a new translation in the de_DE-blog" href="%s"><span class="dashicons dashicons-plus"></span></a>&nbsp;',
$create_link
);
$this->assertEquals( $value, $obj->get_a() );
$this->assertEquals( $value, $obj->__toString() );
}

public function test_get_img_post(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[0];

Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 );
Functions\expect( 'get_admin_url' )->once()->andReturn( $this->admin_url );
Functions\expect( 'get_edit_post_link' )->once()->andReturn( $edit_link );

$post = $this->get_post( $post_type );
$obj = $this->IconFactory( $post );

$this->assertEquals( '<img alt="de_DE" src="' . $this->src . '" />', $obj->get_img() );
$this->assertIsSTring( $obj->get_edit_new() );
}


public function test_get_img_post_page(): void {
list ( $post_type, $create_link, $edit_link ) = $this->get_post_type()[1];

Functions\expect( 'add_query_arg' )->twice()->andReturn( $edit_link );
Functions\expect( 'add_query_arg' )->times( $add_query_arg_calls )->andReturn( $edit_link );
Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 );
Functions\expect( 'get_admin_url' )->once()->andReturn( $this->admin_url );
Functions\expect( 'get_edit_post_link' )->once()->andReturn( $edit_link );

$post = $this->get_post( $post_type );
$obj = $this->IconFactory( $post );
$obj = $this->IconFactory( $this->get_post( $post_type ) );

$this->assertEquals( '<img alt="de_DE" src="' . $this->src . '" />', $obj->get_img() );
$this->assertIsSTring( $obj->get_edit_new() );
$this->assertSame( $this->admin_url, $obj->get_edit_new() );
}

public function test_set_id_with_null_constructor(): void {
Expand Down Expand Up @@ -206,19 +170,6 @@ public function test_get_icon( string $icon_type, ?string $language, string $exp
$this->assertEquals( $expected, $obj->get_icon() );
}

public function test_get_icon_label(): void {
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 3 ) . '/' );

$obj = new Icon( 'post' );
$obj->set_icon_type( 'flag' );

$this->assertEquals( '', $obj->get_icon() );

$obj->set_language( 'de_DE' );

$this->assertEquals( '<span class="flag-icon flag-icon-de">de_DE</span>', $obj->get_icon() );
}

public function test_get_edit_new(): void {
$obj = new Icon( 'post' );
$obj->set_id( 123 );
Expand Down
60 changes: 31 additions & 29 deletions tests/phpunit/Admin/TestMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ public function test_get_suggested_fields_no_posts(): void {
$this->assertEquals( $json, MetaBox::get_suggested_fields( $json, $args ) );
}

public function test_render_option_selected(): void {
Functions\expect( 'selected' )->once()->andReturn( 'selected="selected"' );
Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' );

$test = $this->MetaBoxFactory();

$this->assertEquals( '<option value="1" selected="selected">Test</option>', $test->render_option( 1, 1 ) );
/**
* @return array<string, array{int, string, string}>
*/
public static function render_option_provider(): array {
return array(
'selected' => array( 1, 'selected="selected"', '<option value="1" selected="selected">Test</option>' ),
'not selected' => array( 2, '', '<option value="1" >Test</option>' ),
);
}

public function test_render_option_not_selected(): void {
Functions\expect( 'selected' )->once()->andReturn( '' );
#[DataProvider( 'render_option_provider' )]
public function test_render_option( int $current, string $selected, string $expected ): void {
Functions\expect( 'selected' )->once()->andReturn( $selected );
Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' );

$test = $this->MetaBoxFactory();

$this->assertEquals( '<option value="1" >Test</option>', $test->render_option( 1, 2 ) );
$this->assertEquals( $expected, $this->MetaBoxFactory()->render_option( 1, $current ) );
}

public function test_render_options(): void {
Expand Down Expand Up @@ -367,13 +367,23 @@ public function test_set_with_request(): void {
$this->MetaBoxFactory()->set( 13 );
}

public function test_maybe_set_linked_post() {
$post = \Mockery::mock( 'WP_Post' );

/**
* Expects a request asking to link the post 42 in de_DE. The id is only read when nothing is linked yet.
*/
private function expect_linked_post_request( bool $reads_id ): void {
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );

if ( $reads_id ) {
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
}
}

public function test_maybe_set_linked_post() {
$post = \Mockery::mock( 'WP_Post' );

$this->expect_linked_post_request( true );
Functions\expect( 'get_post' )->once()->andReturn( $post );
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'switch_to_blog' )->once();
Expand All @@ -388,10 +398,7 @@ public function test_maybe_set_linked_post() {
}

public function test_maybe_set_linked_post_with_no_post() {
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
$this->expect_linked_post_request( true );
Functions\expect( 'get_post' )->once()->andReturn( null );
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'switch_to_blog' )->once();
Expand All @@ -405,18 +412,15 @@ public function test_maybe_set_linked_post_with_no_post() {
$this->assertNull( $mydata->de_DE );
}

function test_maybe_set_linked_post_with_no_blog_id() {
public function test_maybe_set_linked_post_with_no_blog_id() {
$options = \Mockery::mock( Options::class );

$collection = \Mockery::mock( Collection::class );
$collection->shouldReceive( 'get_blog_id' )->andReturn( null );

$test = new MetaBox( $options, $collection );

Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
$this->expect_linked_post_request( true );
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new Post();
Expand All @@ -425,16 +429,14 @@ function test_maybe_set_linked_post_with_no_blog_id() {
$this->assertNull( $mydata->de_DE );
}

function test_maybe_set_linked_post_with_mydata_already_set() {
public function test_maybe_set_linked_post_with_mydata_already_set() {
$options = \Mockery::mock( Options::class );

$collection = \Mockery::mock( Collection::class );

$test = new MetaBox( $options, $collection );

Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, Fields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
$this->expect_linked_post_request( false );
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new Post();
Expand Down
Loading
Loading