Skip to content

Commit

Permalink
Merge pull request #372 from lloc/raise-coverage
Browse files Browse the repository at this point in the history
Raise coverage
  • Loading branch information
lloc authored Sep 8, 2024
2 parents 7744cae + ef65f15 commit 8aee872
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 41 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.9.2


## 2.9.1

- Suggest Field in Post Editor Metabox: This feature allows you to input either numeric or alphanumeric values. If you enter a number, it’s treated as a Post ID. If you enter text, the field will suggest posts with matching titles.
Expand Down
4 changes: 2 additions & 2 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @wordpress-plugin
*
* Plugin Name: Multisite Language Switcher
* Version: 2.9.1
* Version: 2.9.2
* Plugin URI: http://msls.co/
* Description: A simple but powerful plugin that will help you to manage the relations of your contents in a multilingual multisite-installation.
* Author: Dennis Ploetner
Expand Down Expand Up @@ -40,7 +40,7 @@
* @author Dennis Ploetner <re@lloc.de>
*/
if ( ! defined( 'MSLS_PLUGIN_VERSION' ) ) {
define( 'MSLS_PLUGIN_VERSION', '2.9.1' );
define( 'MSLS_PLUGIN_VERSION', '2.9.2' );
define( 'MSLS_PLUGIN_PATH', plugin_basename( __FILE__ ) );
define( 'MSLS_PLUGIN__FILE__', __FILE__ );

Expand Down
23 changes: 20 additions & 3 deletions includes/ContentImport/AttachmentPathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ class AttachmentPathFinder extends MslsRegistryInstance {

const LINKED = '_msls_linked';

public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ) {
/**
* @param array $sources
* @param mixed $sizeArray
* @param string $imageSrc
* @param mixed $imageMeta
* @param int $attachmentId
* @return array
*/
public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ): array {
if ( ! $msls_imported = $this->has_import_data( $attachmentId ) ) {
return $sources;
}
Expand All @@ -32,6 +40,10 @@ public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta
return $sources;
}

/**
* @param int $attachment_id
* @return array|false
*/
protected function has_import_data( $attachment_id ) {
if ( empty( $attachment_id ) ) {
return false;
Expand All @@ -53,6 +65,11 @@ protected function has_import_data( $attachment_id ) {
return $msls_imported;
}

/**
* @param string $url
* @param int $attachment_id
* @return string
*/
public function filter_attachment_url( $url, $attachment_id ) {
if ( ! $msls_imported = $this->has_import_data( $attachment_id ) ) {
return $url;
Expand All @@ -68,7 +85,7 @@ public function filter_attachment_url( $url, $attachment_id ) {
}

/**
* @param int $attachment_id
* @param int $attachment_id
* @param array $msls_imported
*
* @return \WP_Post|false
Expand All @@ -84,4 +101,4 @@ protected function get_source_post( $attachment_id, $msls_imported ) {

return $source_post;
}
}
}
18 changes: 14 additions & 4 deletions includes/ContentImport/ContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function get_logger() {
/**
* @param \lloc\Msls\ContentImport\ImportLogger $logger
*/
public function set_logger( $logger ) {
public function set_logger( $logger ): void {
$this->logger = $logger;
}

Expand All @@ -77,7 +77,7 @@ public function get_relations() {
/**
* @param \lloc\Msls\ContentImport\Relations $relations
*/
public function set_relations( $relations ) {
public function set_relations( $relations ): void {
$this->relations = $relations;
}

Expand Down Expand Up @@ -179,6 +179,11 @@ public function parse_sources() {
return array_map( 'intval', $import_data );
}

/**
* @param int $blog_id
*
* @return int
*/
protected function get_the_blog_post_ID( $blog_id ) {
switch_to_blog( $blog_id );

Expand Down Expand Up @@ -331,9 +336,9 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
}

/**
* @param array $data
* @param int $blog_id
* @param int $post_id
*
* @param array $data
* @return array
*/
protected function update_inserted_blog_post_data( $blog_id, $post_id, array $data ) {
Expand All @@ -346,6 +351,11 @@ protected function update_inserted_blog_post_data( $blog_id, $post_id, array $da
return $data;
}

/**
* @param int $dest_blog_id
* @param int $post_id
* @return void
*/
protected function redirect_to_blog_post( $dest_blog_id, $post_id ) {
switch_to_blog( $dest_blog_id );
$edit_post_link = html_entity_decode( get_edit_post_link( $post_id ) );
Expand Down
12 changes: 6 additions & 6 deletions includes/ContentImport/ImportCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ImportCoordinates {
* @var array An array keeping track of which importer (slug) should be used for
* a specific import type, shape [ <import-type> => <slug> ]
*/
public $importers = [];
public $importers = array();

/**
* Validates the coordinates.
Expand Down Expand Up @@ -88,11 +88,11 @@ public function get_importer_for( $importer_type ) {
/**
* Parses the importers from request superglobals.
*/
public function parse_importers_from_request() {
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 ] );
+ isset( $_POST[ self::IMPORTERS_GLOBAL_KEY ] ) * 10
+ isset( $_GET[ self::IMPORTERS_GLOBAL_KEY ] );

switch ( $source ) {
case 100:
Expand All @@ -114,7 +114,7 @@ public function parse_importers_from_request() {

$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 All @@ -127,7 +127,7 @@ public function parse_importers_from_request() {
* @param string $importer_type
* @param string $slug
*/
public function set_importer_for( $importer_type, $slug ) {
public function set_importer_for( $importer_type, $slug ): void {
$this->importers[ $importer_type ] = $slug;
}
}
20 changes: 13 additions & 7 deletions includes/ContentImport/ImportLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ public function get_data(): array {
* Saves the log or prints it some place.
*/
public function save(): void {
$log_writer = $default_log_writer = AdminNoticeLogger::instance();
$log_writer->set_import_coordinates( $this->import_coordinates );
$default_log_writer = AdminNoticeLogger::instance();
$default_log_writer->set_import_coordinates( $this->import_coordinates );

/**
* Filters the log class or object that should be used to write the log to the destination.
*
* @param LogWriter $log_writer
* @param mixed $default_log_writer
* @param ImportCoordinates $import_coordinates
*/
$log_writer = apply_filters( 'msls_content_import_log_writer', $log_writer, $this->import_coordinates );

$log_writer = apply_filters( 'msls_content_import_log_writer', $default_log_writer, $this->import_coordinates );
if ( empty( $log_writer ) ) {
// we assume that was done on purpose to prevent logging
return;
Expand Down Expand Up @@ -106,6 +105,12 @@ protected function log( $where, $what, $root = 'info' ): void {
$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
}

/**
* @param array $path
* @param mixed $what
*
* @return array
*/
protected function build_nested_array( $path, $what = '' ): array {
$json = '{"'
. implode( '":{"', $path )
Expand All @@ -124,11 +129,11 @@ protected function build_nested_array( $path, $what = '' ): array {
}

/**
* @param $where
* @param string $where
*
* @return array
*/
protected function build_path( $where ): array {
protected function build_path( string $where ): array {
$where_path = explode( $this->levels_delimiter, $where );

return $where_path;
Expand Down Expand Up @@ -165,6 +170,7 @@ public function log_success( $where, $what ): void {
/**
* Logs some generic information.
*
* @param string $key
* @param string $message
*/
public function log_information( $key, $message ): void {
Expand Down
14 changes: 9 additions & 5 deletions includes/ContentImport/Importers/ImportersBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function make( ImportCoordinates $import_coordinates ) {
*
* Returning an Importer instance here will force the class to return that.
*
* @param $importer Importer
* @param ?Importer $importer
* @param ImportCoordinates $import_coordinates
*/
$importer = apply_filters( "msls_content_import_{$type}_importer", null, $import_coordinates );
Expand Down Expand Up @@ -81,21 +81,25 @@ public function details() {
* @return string
*/
public function selected() {
$selected = array_keys( $this->importers_map )[0];
$slug = static::TYPE;
$selected = array_keys( $this->importers_map )[0];
$instance = $this;

/**
* Filters the selected importer that among the available ones.
*
* @param string $selected The selected importer slug.
* @param ImportersFactory $this
* @param ImportersFactory $instance
*/
$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $this );
$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $instance );

return $selected;
}

protected function importers_info() {
/**
* @return array<string, \stdClass>
*/
protected function importers_info(): array {
return array_combine(
array_keys( $this->importers_map ),
array_map(
Expand Down
11 changes: 6 additions & 5 deletions includes/ContentImport/Importers/PostFields/Duplicating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lloc\Msls\ContentImport\Importers\PostFields;

use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\ContentImport\Importers\BaseImporter;
use lloc\Msls\ContentImport\Importers\WithRequestPostAttributes;

Expand All @@ -23,11 +24,11 @@ class Duplicating extends BaseImporter {
* @return \stdClass
*/
public static function info() {
return (object) [
return (object) array(
'slug' => static::TYPE,
'name' => __( 'Duplicating', 'multisite-language-switcher' ),
'description' => __( 'Copies the source post fields to the destination.', 'multisite-language-switcher' )
];
'description' => __( 'Copies the source post fields to the destination.', 'multisite-language-switcher' ),
);
}

public function import( array $data ) {
Expand All @@ -39,11 +40,11 @@ public function import( array $data ) {
foreach ( $this->filter_fields() as $field ) {
$value = $source_post->{$field};
$data[ $field ] = $value;
$this->logger->log_success( 'post-field/added', [ $field => $value ] );
$this->logger->log_success( 'post-field/added', array( $field => $value ) );
}

if ( ! doing_action( 'wp_insert_post_data' ) ) {
$postarr = array_merge( $data, [ 'ID' => $this->import_coordinates->dest_post_id ] );
$postarr = array_merge( $data, array( 'ID' => $this->import_coordinates->dest_post_id ) );
wp_insert_post( $postarr );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/MslsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function get_config( string $name ): array {
public static function has_var( string $name, ?int $input_type = null ): bool {
if ( null === $input_type ) {
try {
list($input_type, ) = self::get_config( $name );
list( $input_type, ) = self::get_config( $name );
} catch ( \InvalidArgumentException $e ) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion js/msls-widget-block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icon": "translation",
"category": "widgets",
"name": "lloc/msls-widget-block",
"version": "2.9.1",
"version": "2.9.2",
"description": "Review the settings for the Multisite Language Switcher plugin, as the block utilizes the API function `the_msls()` for its output.",
"example": {},
"supports": {
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tags: multilingual, multisite, language, switcher, localization
Requires at least: 6.1
Tested up to: 6.6
Requires PHP: 7.4
Stable tag: 2.9.1
Stable tag: 2.9.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down
2 changes: 1 addition & 1 deletion src/msls-widget-block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icon": "translation",
"category": "widgets",
"name": "lloc/msls-widget-block",
"version": "2.9.1",
"version": "2.9.2",
"description": "Review the settings for the Multisite Language Switcher plugin, as the block utilizes the API function `the_msls()` for its output.",
"example": {},
"supports": {
Expand Down
26 changes: 26 additions & 0 deletions tests/phpunit/ContentImport/TestContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use lloc\Msls\ContentImport\Relations;
use lloc\Msls\MslsMain;
use lloc\MslsTests\MslsUnitTestCase;
use Brain\Monkey\Actions;

class TestContentImporter extends MslsUnitTestCase {

Expand All @@ -15,6 +16,7 @@ public function setUp(): void {
parent::setUp();

$main = \Mockery::mock( MslsMain::class );
$main->shouldReceive( 'verify_nonce' )->andReturnTrue();

$this->test = new ContentImporter( $main );
}
Expand All @@ -30,4 +32,28 @@ public function test_relations(): void {

$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
}

public function test_handle_import() {
$this->assertEquals( array(), $this->test->handle_import() );
}

public function test_parse_sources_no_post() {
$this->assertFalse( $this->test->parse_sources() );
}

public function test_handle_false() {
$this->expectNotToPerformAssertions();

Actions\expectAdded( 'msls_main_save' )->once();

$this->test->handle( false );
}

public function test_handle_true() {
$this->expectNotToPerformAssertions();

Actions\expectRemoved( 'msls_main_save' )->once();

$this->test->handle( true );
}
}
Loading

0 comments on commit 8aee872

Please sign in to comment.