diff --git a/includes/MslsCustomFilter.php b/includes/MslsCustomFilter.php index 9fd1ce16..a0127df4 100644 --- a/includes/MslsCustomFilter.php +++ b/includes/MslsCustomFilter.php @@ -1,15 +1,12 @@ - * @contributor Dennis Ploetner - * @since 0.9.9 - */ namespace lloc\Msls; +use lloc\Msls\Query\TranslatedPostsQuery; + /** * Adding custom filter to posts/pages table. + * * @package Msls */ class MslsCustomFilter extends MslsMain { @@ -22,15 +19,15 @@ class MslsCustomFilter extends MslsMain { * @return MslsCustomFilter */ public static function init() { - $options = msls_options(); + $options = msls_options(); $collection = msls_blog_collection(); - $obj = new static( $options, $collection ); + $obj = new static( $options, $collection ); if ( ! $options->is_excluded() ) { $post_type = MslsPostType::instance()->get_request(); if ( ! empty( $post_type ) ) { - add_action( 'restrict_manage_posts', [ $obj, 'add_filter' ] ); - add_filter( 'parse_query', [ $obj, 'execute_filter' ] ); + add_action( 'restrict_manage_posts', array( $obj, 'add_filter' ) ); + add_filter( 'parse_query', array( $obj, 'execute_filter' ) ); } } @@ -39,11 +36,12 @@ public static function init() { /** * Echo's select tag with list of blogs + * * @uses selected */ public function add_filter(): void { $id = ( - filter_has_var( INPUT_GET, 'msls_filter' ) ? + filter_has_var( INPUT_GET, 'msls_filter' ) ? filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ) : '' ); @@ -57,8 +55,10 @@ public function add_filter(): void { '', $blog->userblog_id, selected( $id, $blog->userblog_id, false ), - sprintf( __( 'Not translated in the %s-blog', 'multisite-language-switcher' ), - $blog->get_description() ) + sprintf( + __( 'Not translated in the %s-blog', 'multisite-language-switcher' ), + $blog->get_description() + ) ); } echo ''; @@ -80,21 +80,14 @@ public function execute_filter( \WP_Query $query ) { } $id = filter_input( INPUT_GET, 'msls_filter', FILTER_SANITIZE_NUMBER_INT ); - if ( isset( $blogs[ $id ] ) ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); + $sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); - // load post we need to exclude (already have translation) from search query - $posts = $cache->get_results( - $cache->prepare( - "SELECT option_id, option_name FROM {$cache->options} WHERE option_name LIKE %s AND option_value LIKE %s", - 'msls_%', - '%"' . $blogs[ $id ]->get_language() . '"%' - ) - ); + // load post we need to exclude (they already have a translation) from search query + $translated_posts = ( new TranslatedPostsQuery( $sql_cache ) )( $blogs[ $id ]->get_language() ); - $exclude_ids = []; - foreach ( $posts as $post ) { + $exclude_ids = array(); + foreach ( $translated_posts as $post ) { $exclude_ids[] = substr( $post->option_name, 5 ); } $query->query_vars['post__not_in'] = $exclude_ids; @@ -104,5 +97,4 @@ public function execute_filter( \WP_Query $query ) { return false; } - } diff --git a/includes/MslsGetSet.php b/includes/MslsGetSet.php index c5007ada..791ea1cf 100644 --- a/includes/MslsGetSet.php +++ b/includes/MslsGetSet.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -18,15 +19,16 @@ class MslsGetSet extends MslsRegistryInstance { /** * Generic container for all properties of an instance + * * @var array $arr */ - protected $arr = []; + protected $arr = array(); /** * Overloads the set method. * * @param string $key - * @param mixed $value + * @param mixed $value */ public function __set( $key, $value ) { $this->arr[ $key ] = $value; @@ -75,7 +77,7 @@ public function __unset( $key ) { * @return MslsGetSet */ public function reset() { - $this->arr = []; + $this->arr = array(); return $this; } @@ -98,7 +100,7 @@ public function reset() { * * @return bool */ - public function has_value( $key ) { + public function has_value( string $key ): bool { return ! empty( $this->arr[ $key ] ); } @@ -119,5 +121,4 @@ public function is_empty() { public function get_arr() { return $this->arr; } - } diff --git a/includes/MslsOptionsQuery.php b/includes/MslsOptionsQuery.php index f568c9e4..bc5017a6 100644 --- a/includes/MslsOptionsQuery.php +++ b/includes/MslsOptionsQuery.php @@ -1,15 +1,9 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** - * OptionsQuery + * MslsOptionsQuery * * @package Msls */ @@ -22,36 +16,48 @@ class MslsOptionsQuery extends MslsOptions { */ public $with_front = true; + protected MslsSqlCacher $sql_cache; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct(); + + $this->sql_cache = $sql_cache; + } + + /** + * @return array + */ + public static function get_params(): array { + return array(); + } + /** * Factory method * * @param int $id This parameter is unused here * - * @return MslsOptionsQuery|null + * @return ?MslsOptionsQuery */ - public static function create( $id = 0 ) { - $query = null; - + public static function create( $id = 0 ): ?MslsOptionsQuery { if ( is_day() ) { - $query = new MslsOptionsQueryDay( - get_query_var( 'year' ), - get_query_var( 'monthnum' ), - get_query_var( 'day' ) - ); + $query_class = MslsOptionsQueryDay::class; } elseif ( is_month() ) { - $query = new MslsOptionsQueryMonth( - get_query_var( 'year' ), - get_query_var( 'monthnum' ) - ); + $query_class = MslsOptionsQueryMonth::class; } elseif ( is_year() ) { - $query = new MslsOptionsQueryYear( get_query_var( 'year' ) ); + $query_class = MslsOptionsQueryYear::class; } elseif ( is_author() ) { - $query = new MslsOptionsQueryAuthor( get_queried_object_id() ); + $query_class = MslsOptionsQueryAuthor::class; } elseif ( is_post_type_archive() ) { - $query = new MslsOptionsQueryPostType( get_query_var( 'post_type' ) ); + $query_class = MslsOptionsQueryPostType::class; + } + + if ( ! isset( $query_class ) ) { + return null; } - return $query; + $sql_cache = MslsSqlCacher::create( $query_class, $query_class::get_params() ); + + return new $query_class( $sql_cache ); } /** diff --git a/includes/MslsOptionsQueryAuthor.php b/includes/MslsOptionsQueryAuthor.php index d985f544..0b3e9827 100644 --- a/includes/MslsOptionsQueryAuthor.php +++ b/includes/MslsOptionsQueryAuthor.php @@ -1,20 +1,33 @@ - * @since 0.9.8 - */ namespace lloc\Msls; +use lloc\Msls\Query\AuthorPostsCounterQuery; + /** - * OptionsQueryAuthor + * MslsOptionsQueryAuthor * * @package Msls */ class MslsOptionsQueryAuthor extends MslsOptionsQuery { + protected int $author_id; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct( $sql_cache ); + + $this->author_id = self::get_params()['author_id']; + } + + /** + * @return array + */ + public static function get_params(): array { + return array( + 'author_id' => get_queried_object_id(), + ); + } + /** * Check if the array has a non-empty item which has $language as a key * @@ -22,16 +35,9 @@ class MslsOptionsQueryAuthor extends MslsOptionsQuery { * * @return bool */ - public function has_value( $language ) { + public function has_value( string $language ): bool { if ( ! isset( $this->arr[ $language ] ) ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args ); - - $this->arr[ $language ] = $cache->get_var( - $cache->prepare( - "SELECT count(ID) FROM {$cache->posts} WHERE post_author = %d AND post_status = 'publish'", - $this->get_arg( 0, 0 ) - ) - ); + $this->arr[ $language ] = ( new AuthorPostsCounterQuery( $this->sql_cache ) )( $this->author_id ); } return (bool) $this->arr[ $language ]; @@ -43,6 +49,6 @@ public function has_value( $language ) { * @return string */ public function get_current_link() { - return get_author_posts_url( $this->get_arg( 0, 0 ) ); + return get_author_posts_url( $this->author_id ); } } diff --git a/includes/MslsOptionsQueryDay.php b/includes/MslsOptionsQueryDay.php index e26a702d..07c175cb 100644 --- a/includes/MslsOptionsQueryDay.php +++ b/includes/MslsOptionsQueryDay.php @@ -1,39 +1,51 @@ - * @since 0.9.8 - */ namespace lloc\Msls; +use lloc\Msls\Query\DatePostsCounterQuery; + /** - * OptionsQueryDay + * MslsOptionsQueryDay * * @package Msls */ class MslsOptionsQueryDay extends MslsOptionsQuery { + protected int $year; + + protected int $monthnum; + protected int $day; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct( $sql_cache ); + + $params = self::get_params(); + + $this->year = $params['year']; + $this->monthnum = $params['monthnum']; + $this->day = $params['day']; + } + + public static function get_params(): array { + return array( + 'year' => get_query_var( 'year' ), + 'monthnum' => get_query_var( 'monthnum' ), + 'day' => get_query_var( 'day' ), + ); + } + /** - * Check if the array has an non empty item which has $language as a key + * Check if the array has a non-empty item which has $language as a key * * @param string $language * * @return bool */ - public function has_value( $language ) { + public function has_value( string $language ): bool { if ( ! isset( $this->arr[ $language ] ) ) { - $date = new \DateTime(); - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args ); - - $this->arr[ $language ] = $cache->get_var( - $cache->prepare( - "SELECT count(ID) FROM {$cache->posts} WHERE DATE(post_date) = %s AND post_status = 'publish'", - $date->setDate( $this->get_arg( 0, 0 ), - $this->get_arg( 1, 0 ), - $this->get_arg( 2, 0 ) )->format( 'Y-m-d' ) - ) - ); + $query_callable = new DatePostsCounterQuery( $this->sql_cache ); + + $this->arr[ $language ] = $query_callable( $this->year, $this->monthnum, $this->day ); } return (bool) $this->arr[ $language ]; @@ -45,7 +57,6 @@ public function has_value( $language ) { * @return string */ public function get_current_link() { - return get_day_link( $this->get_arg( 0, 0 ), $this->get_arg( 1, 0 ), $this->get_arg( 2, 0 ) ); + return get_day_link( $this->year, $this->monthnum, $this->day ); } - } diff --git a/includes/MslsOptionsQueryMonth.php b/includes/MslsOptionsQueryMonth.php index 51a73cd3..4ca3dc5e 100644 --- a/includes/MslsOptionsQueryMonth.php +++ b/includes/MslsOptionsQueryMonth.php @@ -1,37 +1,50 @@ - * @since 0.9.8 - */ namespace lloc\Msls; +use lloc\Msls\Query\MonthPostsCounterQuery; + /** - * OptionsQueryMonth + * MslsOptionsQueryMonth * * @package Msls */ class MslsOptionsQueryMonth extends MslsOptionsQuery { + protected int $year; + + protected int $monthnum; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct( $sql_cache ); + + $params = self::get_params(); + + $this->year = $params['year']; + $this->monthnum = $params['monthnum']; + } + + /** + * @return array + */ + public static function get_params(): array { + return array( + 'year' => get_query_var( 'year' ), + 'monthnum' => get_query_var( 'monthnum' ), + ); + } + /** - * Check if the array has an non empty item which has $language as a key + * Check if the array has a non-empty item which has $language as a key * * @param string $language * * @return bool */ - public function has_value( $language ) { + public function has_value( string $language ): bool { if ( ! isset( $this->arr[ $language ] ) ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args ); - - $this->arr[ $language ] = $cache->get_var( - $cache->prepare( - "SELECT count(ID) FROM {$cache->posts} WHERE YEAR(post_date) = %d AND MONTH(post_date) = %d AND post_status = 'publish'", - $this->get_arg( 0, 0 ), - $this->get_arg( 1, 0 ) - ) - ); + $this->arr[ $language ] = ( new MonthPostsCounterQuery( $this->sql_cache ) )( $this->year, $this->monthnum ); + } return (bool) $this->arr[ $language ]; @@ -42,8 +55,7 @@ public function has_value( $language ) { * * @return string */ - public function get_current_link() { - return get_month_link( $this->get_arg( 0, 0 ), $this->get_arg( 1, 0 ) ); + public function get_current_link(): string { + return get_month_link( $this->year, $this->monthnum ); } - } diff --git a/includes/MslsOptionsQueryPostType.php b/includes/MslsOptionsQueryPostType.php index 95950d5c..872e4b80 100644 --- a/includes/MslsOptionsQueryPostType.php +++ b/includes/MslsOptionsQueryPostType.php @@ -1,20 +1,28 @@ - * @since 0.9.8 - */ namespace lloc\Msls; /** - * OptionsQueryPostType + * MslsOptionsQueryPostType * * @package Msls */ class MslsOptionsQueryPostType extends MslsOptionsQuery { + protected string $post_type; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct( $sql_cache ); + + $this->post_type = self::get_params()['post_type']; + } + + public static function get_params(): array { + return array( + 'post_type' => get_query_var( 'post_type' ), + ); + } + /** * Check if the array has a non-empty item which has $language as a key * @@ -22,9 +30,9 @@ class MslsOptionsQueryPostType extends MslsOptionsQuery { * * @return bool */ - public function has_value( $language ) { + public function has_value( string $language ): bool { if ( ! isset( $this->arr[ $language ] ) ) { - $this->arr[ $language ] = get_post_type_object( $this->get_arg( 0, '' ) ); + $this->arr[ $language ] = get_post_type_object( $this->post_type ); } return (bool) $this->arr[ $language ]; @@ -36,6 +44,6 @@ public function has_value( $language ) { * @return string */ public function get_current_link() { - return (string) get_post_type_archive_link( $this->get_arg( 0, '' ) ); + return (string) get_post_type_archive_link( $this->post_type ); } } diff --git a/includes/MslsOptionsQueryYear.php b/includes/MslsOptionsQueryYear.php index ad2c062b..75c18cc0 100644 --- a/includes/MslsOptionsQueryYear.php +++ b/includes/MslsOptionsQueryYear.php @@ -1,13 +1,9 @@ - * @since 0.9.8 - */ namespace lloc\Msls; +use lloc\Msls\Query\YearPostsCounterQuery; + /** * OptionsQueryYear * @@ -15,6 +11,20 @@ */ class MslsOptionsQueryYear extends MslsOptionsQuery { + protected int $year; + + public function __construct( MslsSqlCacher $sql_cache ) { + parent::__construct( $sql_cache ); + + $this->year = self::get_params()['year']; + } + + public static function get_params(): array { + return array( + 'year' => get_query_var( 'year' ), + ); + } + /** * Check if the array has a non-empty item which has $language as a key * @@ -22,16 +32,9 @@ class MslsOptionsQueryYear extends MslsOptionsQuery { * * @return bool */ - public function has_value( $language ) { + public function has_value( string $language ): bool { if ( ! isset( $this->arr[ $language ] ) ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( $this->args ); - - $this->arr[ $language ] = $cache->get_var( - $cache->prepare( - "SELECT count(ID) FROM {$cache->posts} WHERE YEAR(post_date) = %d AND post_status = 'publish'", - $this->get_arg( 0, 0 ) - ) - ); + $this->arr[ $language ] = ( new YearPostsCounterQuery( $this->sql_cache ) )( $this->year ); } return (bool) $this->arr[ $language ]; @@ -42,7 +45,7 @@ public function has_value( $language ) { * * @return string */ - public function get_current_link() { - return get_year_link( $this->get_arg( 0, 0 ) ); + public function get_current_link(): string { + return get_year_link( $this->year ); } } diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index 17f300ac..d61f1a77 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -419,7 +419,7 @@ public static function uninstall() { * restore_current_blog */ if ( function_exists( 'is_multisite' ) && is_multisite() ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); + $cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); $blogs = $cache->get_results( $cache->prepare( @@ -449,7 +449,7 @@ public static function uninstall() { */ public static function cleanup() { if ( delete_option( 'msls' ) ) { - $cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); + $cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); $sql = $cache->prepare( "DELETE FROM {$cache->options} WHERE option_name LIKE %s", 'msls_%' diff --git a/includes/MslsSqlCacher.php b/includes/MslsSqlCacher.php index 43ecbb31..c67fa882 100644 --- a/includes/MslsSqlCacher.php +++ b/includes/MslsSqlCacher.php @@ -26,109 +26,80 @@ */ class MslsSqlCacher { + /** + * Cache group + */ + const CACHE_GROUP = 'msls-cache-group'; + /** * Database object - * - * @var object $db */ - protected $db; + protected \wpdb $db; /** - * Name of the object which created this object - * - * @var string $caller + * Key for the cached result-set */ - protected $caller; + protected string $cache_key; /** - * Parameters are used to create the key for the cached resultset - * - * @var mixed $params + * Expiration time for the cache in seconds */ - protected $params; + protected int $expire; /** * Constructor - * - * @param \wpdb $db - * @param string $caller */ - public function __construct( \wpdb $db, string $caller ) { - $this->db = $db; - $this->caller = $caller; + public function __construct( \wpdb $db, string $cache_key, int $expire = 0 ) { + $this->db = $db; + $this->cache_key = $cache_key; + $this->expire = $expire; } /** * Factory * * @param string $caller - * - * @return MslsSqlCacher - * @uses \WPDB $wpdb - * + * @param mixed $params + * @param int $expire */ - public static function init( string $caller ): self { + public static function create( string $caller, $params, int $expire = 0 ): self { global $wpdb; - return new self( $wpdb, $caller ); - } - - /** - * Set params - * - * @param mixed $params - * - * @return MslsSqlCacher - */ - public function set_params( $params ): self { - $this->params = $params; - - return $this; - } - - /** - * Get the name of the key which is in use for the cached object - * - * @return string - */ - public function get_key() { - $params = is_array( $this->params ) ? implode( '_', $this->params ) : $this->params; + if ( is_array( $params ) ) { + $params = implode( '_', $params ); + } - return $this->caller . '_' . $params; + return new self( $wpdb, esc_attr( $caller . '_' . $params ), $expire ); } /** * Magic __get * - * @param string $key - * * @return mixed */ - public function __get( $key ) { - return $this->db->$key ?? null; + public function __get( string $name ) { + return $this->db->$name ?? null; } /** * Call a method of the db-object with the needed args and cache the result * - * @param string $method - * @param string[] $args + * @param string $method + * @param array $args * * @return mixed */ - public function __call( $method, $args ) { + public function __call( string $method, array $args ) { if ( 'get_' != substr( $method, 0, 4 ) ) { - $result = call_user_func_array( [ $this->db, $method ], $args ); - } else { - $key = $this->get_key(); - $result = wp_cache_get( $key ); - if ( false === $result ) { - $result = call_user_func_array( [ $this->db, $method ], $args ); - wp_cache_set( $key, $result ); - } + return call_user_func_array( array( $this->db, $method ), $args ); + } + + $result = wp_cache_get( $this->cache_key, self::CACHE_GROUP ); + if ( false === $result ) { + $result = call_user_func_array( array( $this->db, $method ), $args ); + wp_cache_set( $this->cache_key, $result, self::CACHE_GROUP, $this->expire ); } return $result; } - } diff --git a/includes/Query/AbstractQuery.php b/includes/Query/AbstractQuery.php new file mode 100644 index 00000000..aed9924a --- /dev/null +++ b/includes/Query/AbstractQuery.php @@ -0,0 +1,14 @@ +sql_cache = $sql_cache; + } +} diff --git a/includes/Query/AuthorPostsCounterQuery.php b/includes/Query/AuthorPostsCounterQuery.php new file mode 100644 index 00000000..0618f676 --- /dev/null +++ b/includes/Query/AuthorPostsCounterQuery.php @@ -0,0 +1,20 @@ +sql_cache->prepare( + "SELECT count(ID) FROM {$this->sql_cache->posts} WHERE post_author = %d AND post_status = 'publish'", + $author_id + ); + + return (int) $this->sql_cache->get_var( $query ); + } +} diff --git a/includes/Query/DatePostsCounterQuery.php b/includes/Query/DatePostsCounterQuery.php new file mode 100644 index 00000000..abb16338 --- /dev/null +++ b/includes/Query/DatePostsCounterQuery.php @@ -0,0 +1,22 @@ +setDate( $year, $monthnum, $day ); + + $query = $this->sql_cache->prepare( + "SELECT count(ID) FROM {$this->sql_cache->posts} WHERE DATE(post_date) = %s AND post_status = 'publish'", + $date->format( 'Y-m-d' ) + ); + + return (int) $this->sql_cache->get_var( $query ); + } +} diff --git a/includes/Query/MonthPostsCounterQuery.php b/includes/Query/MonthPostsCounterQuery.php new file mode 100644 index 00000000..2fa9e884 --- /dev/null +++ b/includes/Query/MonthPostsCounterQuery.php @@ -0,0 +1,21 @@ +sql_cache->prepare( + "SELECT count(ID) FROM {$this->sql_cache->posts} WHERE YEAR(post_date) = %d AND MONTH(post_date) = %d AND post_status = 'publish'", + $year, + $monthnum + ); + + return (int) $this->sql_cache->get_var( $query ); + } +} diff --git a/includes/Query/TranslatedPostsQuery.php b/includes/Query/TranslatedPostsQuery.php new file mode 100644 index 00000000..5310ce18 --- /dev/null +++ b/includes/Query/TranslatedPostsQuery.php @@ -0,0 +1,21 @@ +sql_cache->prepare( + "SELECT option_id, option_name FROM {$this->sql_cache->options} WHERE option_name LIKE %s AND option_value LIKE %s", + 'msls_%', + '%"' . $language . '"%' + ); + + return $this->sql_cache->get_results( $query ); + } +} diff --git a/includes/Query/YearPostsCounterQuery.php b/includes/Query/YearPostsCounterQuery.php new file mode 100644 index 00000000..13b00e83 --- /dev/null +++ b/includes/Query/YearPostsCounterQuery.php @@ -0,0 +1,20 @@ +sql_cache->prepare( + "SELECT count(ID) FROM {$this->sql_cache->posts} WHERE YEAR(post_date) = %d AND post_status = 'publish'", + $year + ); + + return (int) $this->sql_cache->get_var( $query ); + } +} diff --git a/tests/Query/TestTranslatedPostsQuery.php b/tests/Query/TestTranslatedPostsQuery.php new file mode 100644 index 00000000..aa1a61bc --- /dev/null +++ b/tests/Query/TestTranslatedPostsQuery.php @@ -0,0 +1,31 @@ +assertEquals( array(), ( new TranslatedPostsQuery( $sql_cache ) )( '' ) ); + } + + public function test_invoke_with_string(): void { + $result = array( + (object) array( + 'option_id' => 1, + 'option_name' => 'msls_123', + ), + ); + + $sql_cache = \Mockery::mock( MslsSqlCacher::class ); + $sql_cache->shouldReceive( 'prepare' )->once(); + $sql_cache->shouldReceive( 'get_results' )->once()->andReturn( $result ); + + $this->assertEquals( $result, ( new TranslatedPostsQuery( $sql_cache ) )( 'de_DE' ) ); + } +} diff --git a/tests/TestMslsOptionsQuery.php b/tests/TestMslsOptionsQuery.php index 223e360d..d1b82366 100644 --- a/tests/TestMslsOptionsQuery.php +++ b/tests/TestMslsOptionsQuery.php @@ -10,15 +10,28 @@ use lloc\Msls\MslsOptionsQueryMonth; use lloc\Msls\MslsOptionsQueryPostType; use lloc\Msls\MslsOptionsQueryYear; +use lloc\Msls\MslsSqlCacher; /** * TestMslsOptionsQuery */ class TestMslsOptionsQuery extends MslsUnitTestCase { + protected function setUp(): void { + parent::setUp(); + + global $wpdb; + + $wpdb = \Mockery::mock( '\wpdb' ); + } + + public function test_get_params() { + $this->assertEquals( array(), MslsOptionsQuery::get_params() ); + } + public function test_create_is_day(): void { Functions\expect( 'is_day' )->once()->andReturn( true ); - Functions\expect( 'get_query_var' )->times( 3 )->andReturnValues( array( 1969, 6, 26 ) ); + Functions\expect( 'get_query_var' )->times( 6 )->andReturnValues( array( 1969, 6, 26 ) ); Functions\expect( 'get_option' )->once(); $this->assertInstanceOf( MslsOptionsQueryDay::class, MslsOptionsQuery::create() ); @@ -26,7 +39,7 @@ public function test_create_is_day(): void { public function test_create_is_month(): void { Functions\expect( 'is_day' )->once()->andReturn( false ); Functions\expect( 'is_month' )->once()->andReturn( true ); - Functions\expect( 'get_query_var' )->twice()->andReturnValues( array( 1969, 6 ) ); + Functions\expect( 'get_query_var' )->times( 4 )->andReturnValues( array( 1969, 6 ) ); Functions\expect( 'get_option' )->once(); $this->assertInstanceOf( MslsOptionsQueryMonth::class, MslsOptionsQuery::create() ); @@ -36,7 +49,7 @@ public function test_create_is_year(): void { Functions\expect( 'is_day' )->once()->andReturn( false ); Functions\expect( 'is_month' )->once()->andReturn( false ); Functions\expect( 'is_year' )->once()->andReturn( true ); - Functions\expect( 'get_query_var' )->once()->andReturn( 1969 ); + Functions\expect( 'get_query_var' )->times( 2 )->andReturn( 1969 ); Functions\expect( 'get_option' )->once(); $this->assertInstanceOf( MslsOptionsQueryYear::class, MslsOptionsQuery::create() ); @@ -47,7 +60,7 @@ public function test_create_is_author(): void { Functions\expect( 'is_month' )->once()->andReturn( false ); Functions\expect( 'is_year' )->once()->andReturn( false ); Functions\expect( 'is_author' )->once()->andReturn( true ); - Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); + Functions\expect( 'get_queried_object_id' )->times( 2 )->andReturn( 42 ); Functions\expect( 'get_option' )->once(); $this->assertInstanceOf( MslsOptionsQueryAuthor::class, MslsOptionsQuery::create() ); @@ -59,7 +72,7 @@ public function test_create_is_post_type_archive(): void { Functions\expect( 'is_year' )->once()->andReturn( false ); Functions\expect( 'is_author' )->once()->andReturn( false ); Functions\expect( 'is_post_type_archive' )->once()->andReturn( true ); - Functions\expect( 'get_query_var' )->once()->andReturn( 'book' ); + Functions\expect( 'get_query_var' )->times( 2 )->andReturn( 'book' ); Functions\expect( 'get_option' )->once(); $this->assertInstanceOf( MslsOptionsQueryPostType::class, MslsOptionsQuery::create() ); @@ -81,12 +94,16 @@ public function test_current_get_postlink(): void { Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); Functions\expect( 'home_url' )->once()->andReturn( $home_url ); - $this->assertEquals( $home_url, ( new MslsOptionsQuery() )->get_postlink( 'de_DE' ) ); + $sql_cache = \Mockery::mock( MslsSqlCacher::class ); + + $this->assertEquals( $home_url, ( new MslsOptionsQuery( $sql_cache ) )->get_postlink( 'de_DE' ) ); } public function test_non_existent_get_postlink(): void { Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); - $this->assertEquals( '', ( new MslsOptionsQuery() )->get_postlink( 'fr_FR' ) ); + $sql_cache = \Mockery::mock( MslsSqlCacher::class ); + + $this->assertEquals( '', ( new MslsOptionsQuery( $sql_cache ) )->get_postlink( 'fr_FR' ) ); } } diff --git a/tests/TestMslsOptionsQueryAuthor.php b/tests/TestMslsOptionsQueryAuthor.php index 455295c5..a4885a67 100644 --- a/tests/TestMslsOptionsQueryAuthor.php +++ b/tests/TestMslsOptionsQueryAuthor.php @@ -5,23 +5,32 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsQueryAuthor; +use lloc\Msls\MslsSqlCacher; class TestMslsOptionsQueryAuthor extends MslsUnitTestCase { - protected function setUp(): void { - Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] ); + protected function get_test( int $author_id ): MslsOptionsQueryAuthor { + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_queried_object_id' )->once()->andReturn( $author_id ); - $this->test = new MslsOptionsQueryAuthor(); + $sql_cacher = \Mockery::mock( MslsSqlCacher::class ); + $sql_cacher->shouldReceive( 'prepare' )->andReturn( 'SQL Query String' ); + $sql_cacher->shouldReceive( 'get_var' )->andReturn( random_int( 1, 10 ) ); + + return new MslsOptionsQueryAuthor( $sql_cacher ); + } + + public function test_has_value_true(): void { + $this->assertTrue( $this->get_test( 17 )->has_value( 'de_DE' ) ); } - public function test_has_value_method(): void { - $this->assertIsBool( $this->test->has_value( 'de_DE' ) ); + public function test_has_value_false(): void { + $this->assertFalse( $this->get_test( 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link_method(): void { Functions\expect( 'get_author_posts_url' )->once()->andReturn( 'https://example.org/queried-author' ); - $this->assertEquals( 'https://example.org/queried-author', $this->test->get_current_link() ); + $this->assertEquals( 'https://example.org/queried-author', $this->get_test( 42 )->get_current_link() ); } - } diff --git a/tests/TestMslsOptionsQueryDay.php b/tests/TestMslsOptionsQueryDay.php index 1a3ea4f7..008bd134 100644 --- a/tests/TestMslsOptionsQueryDay.php +++ b/tests/TestMslsOptionsQueryDay.php @@ -5,28 +5,37 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsQueryDay; +use lloc\Msls\MslsSqlCacher; /** * TestMslsOptionsQueryDay */ class TestMslsOptionsQueryDay extends MslsUnitTestCase { - public function setUp(): void { + public function get_test( int $year, int $monthnum, int $day ): MslsOptionsQueryDay { parent::setUp(); - Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_query_var' )->times( 3 )->andReturn( $year, $monthnum, $day ); - $this->test = new MslsOptionsQueryDay(); + $sql_cacher = \Mockery::mock( MslsSqlCacher::class ); + $sql_cacher->shouldReceive( 'prepare' )->andReturn( 'SQL Query String' ); + $sql_cacher->shouldReceive( 'get_var' )->andReturn( random_int( 1, 10 ) ); + + return new MslsOptionsQueryDay( $sql_cacher ); + } + + public function test_has_value_true(): void { + $this->assertTrue( $this->get_test( 1998, 12, 31 )->has_value( 'de_DE' ) ); } public function test_has_value(): void { - $this->assertTrue( $this->test->has_value( 'de_DE' ) ); + $this->assertFalse( $this->get_test( 0, 0, 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_day_link' )->once()->andReturn( 'https://example.org/queried-day' ); - $this->assertEquals( 'https://example.org/queried-day', $this->test->get_current_link() ); + $this->assertEquals( 'https://example.org/queried-day', $this->get_test( 2015, 07, 02 )->get_current_link() ); } - } diff --git a/tests/TestMslsOptionsQueryMonth.php b/tests/TestMslsOptionsQueryMonth.php index 28148c55..0d731380 100644 --- a/tests/TestMslsOptionsQueryMonth.php +++ b/tests/TestMslsOptionsQueryMonth.php @@ -5,25 +5,34 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsQueryMonth; +use lloc\Msls\MslsSqlCacher; class TestMslsOptionsQueryMonth extends MslsUnitTestCase { - public function setUp(): void { + public function get_test( int $year, int $monthnum ): MslsOptionsQueryMonth { parent::setUp(); - Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_query_var' )->times( 2 )->andReturn( $year, $monthnum ); - $this->test = new MslsOptionsQueryMonth(); + $sql_cacher = \Mockery::mock( MslsSqlCacher::class ); + $sql_cacher->shouldReceive( 'prepare' )->andReturn( 'SQL Query String' ); + $sql_cacher->shouldReceive( 'get_var' )->andReturn( random_int( 1, 10 ) ); + + return new MslsOptionsQueryMonth( $sql_cacher ); + } + + public function test_has_value_true(): void { + $this->assertTrue( $this->get_test( 1998, 12 )->has_value( 'de_DE' ) ); } - public function test_has_value(): void { - $this->assertIsBool( $this->test->has_value( 'de_DE' ) ); + public function test_has_value_false(): void { + $this->assertFalse( $this->get_test( 0, 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_month_link' )->once()->andReturn( 'https://example.org/queried-month' ); - $this->assertEquals( 'https://example.org/queried-month', $this->test->get_current_link() ); + $this->assertEquals( 'https://example.org/queried-month', $this->get_test( 2015, 7 )->get_current_link() ); } - } diff --git a/tests/TestMslsOptionsQueryPostType.php b/tests/TestMslsOptionsQueryPostType.php index 55f7c351..20b7d6c5 100644 --- a/tests/TestMslsOptionsQueryPostType.php +++ b/tests/TestMslsOptionsQueryPostType.php @@ -5,6 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsQueryPostType; +use lloc\Msls\MslsSqlCacher; /** * TestMslsOptionsQueryPostType @@ -14,12 +15,25 @@ class TestMslsOptionsQueryPostType extends MslsUnitTestCase { protected function setUp(): void { parent::setUp(); - Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_query_var' )->once()->andReturn( 'queried-posttype' ); - $this->test = new MslsOptionsQueryPostType(); + $sql_cacher = \Mockery::mock( MslsSqlCacher::class ); + $sql_cacher->shouldReceive( 'prepare' )->never(); + $sql_cacher->shouldReceive( 'get_var' )->never(); + + $this->test = new MslsOptionsQueryPostType( $sql_cacher ); } public function test_has_value_existing(): void { + Functions\expect( 'get_post_type_object' )->once()->andReturn( + (object) array( + 'labels' => array( + 'name' => 'Post Type Name', + 'singular_name' => 'Post Type Singular Name', + ), + ) + ); $this->assertTrue( $this->test->has_value( 'de_DE' ) ); } diff --git a/tests/TestMslsOptionsQueryYear.php b/tests/TestMslsOptionsQueryYear.php index 1a9cbd99..92a53540 100644 --- a/tests/TestMslsOptionsQueryYear.php +++ b/tests/TestMslsOptionsQueryYear.php @@ -5,25 +5,34 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsQueryYear; +use lloc\Msls\MslsSqlCacher; class TestMslsOptionsQueryYear extends MslsUnitTestCase { - protected function setUp(): void { + protected function get_test( int $year ): MslsOptionsQueryYear { parent::setUp(); - Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_query_var' )->once()->andReturn( $year ); - $this->test = new MslsOptionsQueryYear(); + $sql_cacher = \Mockery::mock( MslsSqlCacher::class ); + $sql_cacher->shouldReceive( 'prepare' )->andReturn( 'SQL Query String' ); + $sql_cacher->shouldReceive( 'get_var' )->andReturn( random_int( 0, 10 ) ); + + return new MslsOptionsQueryYear( $sql_cacher ); + } + + public function test_has_value_true(): void { + $this->assertTrue( $this->get_test( 1998 )->has_value( 'de_DE' ) ); } - public function test_has_value_method(): void { - $this->assertIsBool( $this->test->has_value( 'de_DE' ) ); + public function test_has_value_false(): void { + $this->assertFalse( $this->get_test( 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link_method(): void { Functions\expect( 'get_year_link' )->once()->andReturn( 'https://example.org/queried-year' ); - $this->assertEquals( 'https://example.org/queried-year', $this->test->get_current_link() ); + $this->assertEquals( 'https://example.org/queried-year', $this->get_test( 2015 )->get_current_link() ); } - } diff --git a/tests/TestMslsSqlCacher.php b/tests/TestMslsSqlCacher.php index 361ecb25..3992d074 100644 --- a/tests/TestMslsSqlCacher.php +++ b/tests/TestMslsSqlCacher.php @@ -9,33 +9,37 @@ class TestMslsSqlCacher extends MslsUnitTestCase { protected function setUp(): void { $wpdb = \Mockery::mock( \WPDB::class ); - $wpdb->shouldReceive( [ - 'prepare' => '', - 'get_results' => [] - ] ); + + $wpdb->shouldReceive( 'prepare' )->andReturn( '' ); + $wpdb->shouldReceive( 'get_results' )->andReturn( array() ); $this->test = new MslsSqlCacher( $wpdb, 'MslsSqlCacherTest' ); } + public function test_create() { + global $wpdb; + + Functions\expect( 'esc_attr' )->atLeast()->once()->andReturnFirstArg(); + + $wpdb = \Mockery::mock( \WPDB::class ); + + $this->assertInstanceOf( MslsSqlCacher::class, MslsSqlCacher::create( 'MslsSqlCacherTest', '' ) ); + $this->assertInstanceOf( MslsSqlCacher::class, MslsSqlCacher::create( 'MslsSqlCacherTest', 'abc' ) ); + $this->assertInstanceOf( MslsSqlCacher::class, MslsSqlCacher::create( 'MslsSqlCacherTest', array() ) ); + $this->assertInstanceOf( MslsSqlCacher::class, MslsSqlCacher::create( 'MslsSqlCacherTest', array( 'abc', 'def' ) ) ); + } + public function test_set_params_method(): void { Functions\when( 'wp_cache_get' )->justReturn( false ); Functions\when( 'wp_cache_set' )->justReturn( true ); - $this->assertInstanceOf( MslsSqlCacher::class, $this->test->set_params( array( 'Cache', 'Test' ) ) ); - $this->assertIsSTring( $this->test->get_key() ); - $this->assertEquals( 'MslsSqlCacherTest_Cache_Test', $this->test->get_key() ); - - $this->assertInstanceOf( MslsSqlCacher::class, $this->test->set_params( 'Cache_Test' ) ); - $this->assertIsSTring( $this->test->get_key() ); - $this->assertEquals( 'MslsSqlCacherTest_Cache_Test', $this->test->get_key() ); - $sql = $this->test->prepare( "SELECT blog_id FROM {$this->test->blogs} WHERE blog_id != %d AND site_id = %d", $this->test->blogid, $this->test->siteid ); + $this->assertIsSTring( $sql ); $this->assertIsArray( $this->test->get_results( $sql ) ); } - }