diff --git a/src/Basic_Requirement_Checker.php b/src/Basic_Requirement_Checker.php index f7912b657ab3ac94ae4037ec6a404f24d3537d4f..62adc64a5a076cf404a1c0599dbd3bed96000401 100644 --- a/src/Basic_Requirement_Checker.php +++ b/src/Basic_Requirement_Checker.php @@ -15,6 +15,8 @@ const PLUGIN_INFO_KEY_NICE_NAME = 'nice_name'; const PLUGIN_INFO_KEY_NAME = 'name'; + const PLUGIN_INFO_VERSION = 'version'; + const PLUGIN_INFO_REQUIRED_VERSION = 'required_version'; /** @var string */ protected $plugin_name; @@ -38,10 +40,6 @@ protected $notices; /** @var @string */ private $text_domain; - /** @var string */ - private $minimum_required_plugin_version; - /** @var string */ - private $force_plugin_update; /** * @param string $plugin_file @@ -49,15 +47,11 @@ * @param string $text_domain * @param string $php_version * @param string $wp_version - * @param string $minimum_required_plugin_version - * @param bool $force_plugin_update */ - public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version, $minimum_required_plugin_version, $force_plugin_update = false ) { + public function __construct( $plugin_file, $plugin_name, $text_domain, $php_version, $wp_version ) { $this->plugin_file = $plugin_file; $this->plugin_name = $plugin_name; $this->text_domain = $text_domain; - $this->minimum_required_plugin_version = $minimum_required_plugin_version; - $this->force_plugin_update = $force_plugin_update; $this->set_min_php_require( $php_version ); $this->set_min_wp_require( $wp_version ); @@ -112,36 +106,18 @@ return $this; } - /** - * @param $minimum_require_plugin_version - * - * @return WPDesk_Basic_Requirement_Checker - */ - public function set_minimum_require_plugin_version( $minimum_require_plugin_version ) { - $this->minimum_required_plugin_version = $minimum_require_plugin_version; - return $this; - } - - /** - * @param $force_plugin_update - * - * @return WPDesk_Basic_Requirement_Checker - */ - public function force_plugin_update( $force_plugin_update ) { - $this->force_plugin_update = $force_plugin_update; - return $this; - } - /** * @param string $plugin_name Name in wp format dir/file.php * @param string $nice_plugin_name Nice plugin name for better looks in notice + * @param string $plugin_require_version required plugin minimum version * * @return $this */ - public function add_plugin_require( $plugin_name, $nice_plugin_name = null ) { + public function add_plugin_require( $plugin_name, $nice_plugin_name = null, $plugin_require_version = null ) { $this->plugin_require[ $plugin_name ] = array( self::PLUGIN_INFO_KEY_NAME => $plugin_name, - self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name + self::PLUGIN_INFO_KEY_NICE_NAME => $nice_plugin_name === null ? $plugin_name : $nice_plugin_name, + self::PLUGIN_INFO_VERSION => $plugin_require_version === null ? '0.0' : $plugin_require_version, ); return $this; @@ -228,14 +204,11 @@ $this->get_text_domain() ), esc_html( $this->plugin_name ), '0x' . dechex( $this->min_openssl_version ) ) ); } - if ( false !== $this->check_minimum_required_plugin_version( $this->minimum_required_plugin_version ) ) { - $notices[] = $this->prepare_notice_message( sprintf( __('The “%s” plugin requires at least %s version to work correctly with WooCommerce. Please update it', $this->get_text_domain() ), - esc_html( $this->plugin_name ), $this->minimum_required_plugin_version ) ); - } $notices = $this->append_plugin_require_notices( $notices ); $notices = $this->append_module_require_notices( $notices ); $notices = $this->append_settings_require_notices( $notices ); + $notices = $this->check_minimum_require_plugins_version_and_display_notices( $notices ); return $notices; } @@ -308,54 +281,53 @@ } /** - * @param $required_minimum_version + * @param $notices * - * @return bool + * @return array */ - public function check_minimum_required_plugin_version( $required_minimum_version ) { - if ( null === $this->get_current_plugin_version() ) { - return false; + private function check_minimum_require_plugins_version_and_display_notices( $notices ) { + + if ( $this->require_plugins() > 0 ) { + foreach ( $this->require_plugins() as $plugin ) { + if ( $plugin[ ucfirst( self::PLUGIN_INFO_VERSION ) ] < $plugin[ self::PLUGIN_INFO_REQUIRED_VERSION ] ) { + $notices[] = $this->prepare_notice_message( sprintf( __( 'The “%s” plugin requires at least %s version of %s to work correctly. Please update it', $this->get_text_domain() ), + esc_html( $this->plugin_name ), $plugin[ self::PLUGIN_INFO_REQUIRED_VERSION ], $plugin[ ucfirst( self::PLUGIN_INFO_KEY_NAME ) ] ) ); + } + } } - return $this->get_current_plugin_version() > $required_minimum_version; + return $notices; } /** + * Check the plugins directory and retrieve all required plugin files with plugin data. + * * @return array */ - private function get_existing_plugins() { - if( ! function_exists( 'get_plugins' ) ) { - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - } + public function require_plugins() { + $require_plugins = array(); - return ( get_plugins() ? get_plugins() : [] ); - } - - /** - * @return mixed|null - */ - public function get_current_plugin_version() - { - $version = null; - if (! empty( $this->get_existing_plugins() ) ) { - foreach ( $this->get_existing_plugins() as $plugin ) { - if ( $plugin['Name'] === $this->plugin_name ) { - $version = $plugin['Version']; + if ( file_exists( ABSPATH . '/wp-admin/includes/plugin.php' ) ) { + if ( ! function_exists( 'get_plugins' ) ) { + require_once ABSPATH . '/wp-admin/includes/plugin.php'; + } + $get_existing_plugins = ( get_plugins() ? get_plugins() : array() ); + + if ( ! empty( $this->plugin_require ) ) { + foreach ( $this->plugin_require as $plugin ) { + if ( ! isset( $plugin[ self::PLUGIN_INFO_VERSION ] ) ) { + unset( $this->plugin_require[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ] ); + } else { + if ( self::is_wp_plugin_active( $plugin[ self::PLUGIN_INFO_KEY_NAME ] ) ) { + $require_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ] = $get_existing_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ]; + $require_plugins[ $plugin[ self::PLUGIN_INFO_KEY_NAME ] ][ self::PLUGIN_INFO_REQUIRED_VERSION ] = $plugin[ self::PLUGIN_INFO_VERSION ]; + } + } } } } - return $version; - } - - /** - * @return string - */ - private function notice_or_error() - { - $class = true == $this->force_plugin_update ? 'error' : 'notice notice-warning is-dismissible'; - - return $class; + return $require_plugins; } /** @@ -393,7 +365,7 @@ * @return string */ private function prepare_plugin_repository_install_url( $plugin_info ) { - $slug = basename( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ); + $slug = basename( $plugin_info[ self::PLUGIN_INFO_KEY_NAME ] ); $install_url = self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ); if ( function_exists( 'wp_nonce_url' ) && function_exists( 'wp_create_nonce' ) ) { $install_url = wp_nonce_url( $install_url, 'install-plugin_' . $slug ); diff --git a/src/Basic_Requirement_Checker_Factory.php b/src/Basic_Requirement_Checker_Factory.php index e46cf86895d0a04bfb3e0fae31b37135db490393..72289bcbd7489b558f2fc20a7ea4a304d167762e 100644 --- a/src/Basic_Requirement_Checker_Factory.php +++ b/src/Basic_Requirement_Checker_Factory.php @@ -1,100 +1,98 @@ <?php - -if ( ! class_exists( 'Basic_Requirement_Checker' ) ) { - require_once 'Basic_Requirement_Checker.php'; -} - -if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker_With_Update_Disable' ) ) { - require_once 'Basic_Requirement_Checker_With_Update_Disable.php'; -} - -/** - * Falicitates createion of requirement checker - */ -class WPDesk_Basic_Requirement_Checker_Factory { - const LIBRARY_TEXT_DOMAIN = 'wp-basic-requirements'; - - /** - * Creates a simplest possible version of requirement checker. - * - * @param string $plugin_file - * @param string $plugin_name - * @param string $minimum_required_plugin_version - * @param string|null $text_domain Text domain to use. If null try to use library text domain. - * - * @return WPDesk_Requirement_Checker - */ - public function create_requirement_checker( $plugin_file, $plugin_name, $text_domain = null, $minimum_required_plugin_version ) { - return new WPDesk_Basic_Requirement_Checker( $plugin_file, $plugin_name, - $this->initialize_translations( $text_domain ), null, null, $minimum_required_plugin_version ); + + if ( ! class_exists( 'Basic_Requirement_Checker' ) ) { + require_once 'Basic_Requirement_Checker.php'; + } + + if ( ! class_exists( 'WPDesk_Basic_Requirement_Checker_With_Update_Disable' ) ) { + require_once 'Basic_Requirement_Checker_With_Update_Disable.php'; } - + /** - * Creates a requirement checker according to given requirements array info. - * - * @param string $plugin_file - * @param string $plugin_name - * @param string $text_domain Text domain to use. If null try to use library text domain. - * @param array $requirements Requirements array as given by plugin. - * - * @return WPDesk_Requirement_Checker + * Falicitates createion of requirement checker */ - public function create_from_requirement_array( $plugin_file, $plugin_name, $requirements, $text_domain = null ) { - $requirements_checker = new WPDesk_Basic_Requirement_Checker_With_Update_Disable( - $plugin_file, - $plugin_name, - $this->initialize_translations( $text_domain ), - $requirements['php'], - $requirements['wp'], - $requirements['minimum_required_plugin_version'] - ); - - if ( isset( $requirements['plugins'] ) ) { - foreach ( $requirements['plugins'] as $requirement ) { - $requirements_checker->add_plugin_require( $requirement['name'], $requirement['nice_name'] ); - } + class WPDesk_Basic_Requirement_Checker_Factory { + const LIBRARY_TEXT_DOMAIN = 'wp-basic-requirements'; + + /** + * Creates a simplest possible version of requirement checker. + * + * @param string $plugin_file + * @param string $plugin_name + * @param string|null $text_domain Text domain to use. If null try to use library text domain. + * + * @return WPDesk_Requirement_Checker + */ + public function create_requirement_checker( $plugin_file, $plugin_name, $text_domain = null ) { + return new WPDesk_Basic_Requirement_Checker( $plugin_file, $plugin_name, + $this->initialize_translations( $text_domain ), null, null ); } - - if ( isset( $requirements['repo_plugins'] ) ) { - foreach ( $requirements['repo_plugins'] as $requirement ) { - $requirements_checker->add_plugin_repository_require( $requirement['name'], $requirement['version'], - $requirement['nice_name'] ); + + /** + * Creates a requirement checker according to given requirements array info. + * + * @param string $plugin_file + * @param string $plugin_name + * @param string $text_domain Text domain to use. If null try to use library text domain. + * @param array $requirements Requirements array as given by plugin. + * + * @return WPDesk_Requirement_Checker + */ + public function create_from_requirement_array( $plugin_file, $plugin_name, $requirements, $text_domain = null ) { + $requirements_checker = new WPDesk_Basic_Requirement_Checker_With_Update_Disable( + $plugin_file, + $plugin_name, + $this->initialize_translations( $text_domain ), + $requirements['php'], + $requirements['wp'] + ); + + if ( isset( $requirements['plugins'] ) ) { + foreach ( $requirements['plugins'] as $requirement ) { + $requirements_checker->add_plugin_require( $requirement['name'], $requirement['nice_name'], $requirement['version'] ); + } } - } - - if ( isset( $requirements['modules'] ) ) { - foreach ( $requirements['modules'] as $requirement ) { - $requirements_checker->add_php_module_require( $requirement['name'], $requirement['nice_name'] ); + + if ( isset( $requirements['repo_plugins'] ) ) { + foreach ( $requirements['repo_plugins'] as $requirement ) { + $requirements_checker->add_plugin_repository_require( $requirement['name'], $requirement['version'], + $requirement['nice_name'] ); + } } - } - - return $requirements_checker; - } - - /** - * Tries to initialize translations for requirement checker. If not given then default library translation is used. - * - * @param string|null $text_domain - * - * @return string - */ - private function initialize_translations( $text_domain = null ) { - if ( $text_domain === null ) { - $text_domain = self::LIBRARY_TEXT_DOMAIN; - - if ( function_exists( 'determine_locale' ) ) { - $locale = determine_locale(); - } else { // before WP 5.0 compatibility - $locale = get_locale(); + + if ( isset( $requirements['modules'] ) ) { + foreach ( $requirements['modules'] as $requirement ) { + $requirements_checker->add_php_module_require( $requirement['name'], $requirement['nice_name'] ); + } } - $locale = apply_filters( 'plugin_locale', $locale, self::LIBRARY_TEXT_DOMAIN ); - - $lang_mo_file = __DIR__ . '/../lang/' . self::LIBRARY_TEXT_DOMAIN . '-' . $locale . '.mo'; - if ( file_exists( $lang_mo_file ) ) { - load_textdomain( self::LIBRARY_TEXT_DOMAIN, $lang_mo_file ); + + return $requirements_checker; + } + + /** + * Tries to initialize translations for requirement checker. If not given then default library translation is used. + * + * @param string|null $text_domain + * + * @return string + */ + private function initialize_translations( $text_domain = null ) { + if ( $text_domain === null ) { + $text_domain = self::LIBRARY_TEXT_DOMAIN; + + if ( function_exists( 'determine_locale' ) ) { + $locale = determine_locale(); + } else { // before WP 5.0 compatibility + $locale = get_locale(); + } + $locale = apply_filters( 'plugin_locale', $locale, self::LIBRARY_TEXT_DOMAIN ); + + $lang_mo_file = __DIR__ . '/../lang/' . self::LIBRARY_TEXT_DOMAIN . '-' . $locale . '.mo'; + if ( file_exists( $lang_mo_file ) ) { + load_textdomain( self::LIBRARY_TEXT_DOMAIN, $lang_mo_file ); + } } + + return $text_domain; } - - return $text_domain; - } -} + } \ No newline at end of file diff --git a/src/Requirement_Checker.php b/src/Requirement_Checker.php index 8b036d9031ce013a79d33d8426bdd8ecea16600b..033501926396359baa457b0b02229792a59077bd 100644 --- a/src/Requirement_Checker.php +++ b/src/Requirement_Checker.php @@ -1,94 +1,81 @@ <?php -/** - * Checks requirements for plugin - * have to be compatible with PHP 5.2.x - */ -interface WPDesk_Requirement_Checker -{ - /** - * @param string $version - * - * @return $this - */ - public function set_min_php_require($version); - - /** - * @param string $version - * - * @return $this - */ - public function set_min_wp_require($version); - - /** - * @param string $version - * - * @return $this - */ - public function set_min_wc_require($version); - - /** - * @param $version - * - * @return $this - */ - public function set_min_openssl_require($version); - /** - * @param $minimum_require_plugin_version - * - * @return $this + * Checks requirements for plugin + * have to be compatible with PHP 5.2.x */ - public function set_minimum_require_plugin_version($minimum_require_plugin_version); - - /** - * @param $force_plugin_update - * - * @return $this - */ - public function force_plugin_update($force_plugin_update); - /** - * @param string $plugin_name - * @param string $nice_plugin_name Nice plugin name for better looks in notice - * - * @return $this - */ - public function add_plugin_require($plugin_name, $nice_plugin_name = null); - - /** - * @param string $module_name - * @param string $nice_name Nice module name for better looks in notice - * - * @return $this - */ - public function add_php_module_require($module_name, $nice_name = null); - - /** - * @param string $setting - * @param mixed $value - * - * @return $this - */ - public function add_php_setting_require($setting, $value); - - /** - * @return bool - */ - public function are_requirements_met(); - - /** - * @return void - */ - public function disable_plugin_render_notice(); - - /** - * @return void - */ - public function render_notices(); - - /** - * Renders requirement notices in admin panel - * - * @return void - */ - public function disable_plugin(); -} \ No newline at end of file + interface WPDesk_Requirement_Checker + { + /** + * @param string $version + * + * @return $this + */ + public function set_min_php_require($version); + + /** + * @param string $version + * + * @return $this + */ + public function set_min_wp_require($version); + + /** + * @param string $version + * + * @return $this + */ + public function set_min_wc_require($version); + + /** + * @param $version + * + * @return $this + */ + public function set_min_openssl_require($version); + + /** + * @param string $plugin_name + * @param string $nice_plugin_name Nice plugin name for better looks in notice + * + * @return $this + */ + public function add_plugin_require($plugin_name, $nice_plugin_name = null); + + /** + * @param string $module_name + * @param string $nice_name Nice module name for better looks in notice + * + * @return $this + */ + public function add_php_module_require($module_name, $nice_name = null); + + /** + * @param string $setting + * @param mixed $value + * + * @return $this + */ + public function add_php_setting_require($setting, $value); + + /** + * @return bool + */ + public function are_requirements_met(); + + /** + * @return void + */ + public function disable_plugin_render_notice(); + + /** + * @return void + */ + public function render_notices(); + + /** + * Renders requirement notices in admin panel + * + * @return void + */ + public function disable_plugin(); + } \ No newline at end of file diff --git a/tests/unit/Test_Basic_Requirement_Checker.php b/tests/unit/Test_Basic_Requirement_Checker.php index 499fe5102b21a37fafb564586ba50e088c75508a..74140cb6c4b67c9b264cdc9d30cdfba2501ab895 100644 --- a/tests/unit/Test_Basic_Requirement_Checker.php +++ b/tests/unit/Test_Basic_Requirement_Checker.php @@ -1,226 +1,215 @@ <?php - -class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { - const RANDOM_PLUGIN_FILE = 'file'; - - const RANDOM_PLUGIN_NAME = 'name'; - - const RANDOM_PLUGIN_TEXTDOMAIN = 'text'; - - const ALWAYS_VALID_PHP_VERSION = '5.2'; - - const ALWAYS_NOT_VALID_PHP_VERSION = '100.100'; - - const ALWAYS_VALID_WP_VERSION = '4.0'; - - const HOOK_TYPE_ACTION = 'action'; - const MINIMUM_REQUIRED_PLUGIN_VERSION = '1.0'; - - public function setUp() { - WP_Mock::setUp(); - - WP_Mock::wpFunction( 'get_bloginfo' ) - ->andReturn( self::ALWAYS_VALID_WP_VERSION ); - } - - public function tearDown() { - WP_Mock::tearDown(); - } - - public function test_php_version_check() { - $known_PHP_versions = array( '7.3', '7.2', '7.1', '7.0', '5.6', '5.5', '5.4', '5.3', '5.2' ); - - $requirements = $this->create_requirements_for_php_wp( - self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - foreach ( $known_PHP_versions as $version ) { - $requirements->set_min_php_require( $version ); - if ( version_compare( PHP_VERSION, $version, '>=' ) ) { - $this->assertTrue( $requirements->are_requirements_met(), - 'Should be ok because WP is OK and PHP is OK' ); - } else { - $this->assertFalse( $requirements->are_requirements_met(), - 'Should fail because required PHP should be at least ' . $version ); + class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { + const RANDOM_PLUGIN_FILE = 'file'; + + const RANDOM_PLUGIN_NAME = 'name'; + + const RANDOM_PLUGIN_TEXTDOMAIN = 'text'; + + const ALWAYS_VALID_PHP_VERSION = '5.2'; + + const ALWAYS_NOT_VALID_PHP_VERSION = '100.100'; + + const ALWAYS_VALID_WP_VERSION = '4.0'; + + const HOOK_TYPE_ACTION = 'action'; + + public function setUp() { + WP_Mock::setUp(); + + WP_Mock::wpFunction( 'get_bloginfo' ) + ->andReturn( self::ALWAYS_VALID_WP_VERSION ); + } + + public function tearDown() { + WP_Mock::tearDown(); + } + + public function test_php_version_check() { + $known_PHP_versions = array( '7.3', '7.2', '7.1', '7.0', '5.6', '5.5', '5.4', '5.3', '5.2' ); + + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + foreach ( $known_PHP_versions as $version ) { + $requirements->set_min_php_require( $version ); + if ( version_compare( PHP_VERSION, $version, '>=' ) ) { + $this->assertTrue( $requirements->are_requirements_met(), + 'Should be ok because WP is OK and PHP is OK' ); + } else { + $this->assertFalse( $requirements->are_requirements_met(), + 'Should fail because required PHP should be at least ' . $version ); + } } + $requirements->set_min_php_require( self::ALWAYS_NOT_VALID_PHP_VERSION ); + $requirements->are_requirements_met(); + $this->expectOutputRegex( "/PHP/" ); + $requirements->handle_render_notices_action(); } - $requirements->set_min_php_require( self::ALWAYS_NOT_VALID_PHP_VERSION ); - $requirements->are_requirements_met(); - $this->expectOutputRegex( "/PHP/" ); - $requirements->handle_render_notices_action(); - } - - - - /** - * @param string $php - * @param string $wp - * - * @return WPDesk_Basic_Requirement_Checker - */ - private function create_requirements_for_php_wp( $php, $wp ) { - return new WPDesk_Basic_Requirement_Checker( self::RANDOM_PLUGIN_FILE, self::RANDOM_PLUGIN_NAME, - self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp, self::MINIMUM_REQUIRED_PLUGIN_VERSION ); - } - - public function test_wp_version_check() { - $wp_version_fail = '4.1'; - - $requirements = $this->create_requirements_for_php_wp( - self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - $this->assertTrue( $requirements->are_requirements_met(), 'Should be ok because WP is OK and PHP is OK' ); - $requirements->set_min_wp_require( $wp_version_fail ); - $this->assertFalse( $requirements->are_requirements_met(), - 'Should fail because required WP should be at least ' . $wp_version_fail ); - - $this->expectOutputRegex( "/WordPress/" ); - $requirements->handle_render_notices_action(); - } - - public function test_minimum_plugin_version_check() { - $minimum_plugin_version_fail = '0.1'; - - $requirements = $this->create_requirements_for_php_wp( - self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - $this->assertTrue( $requirements->are_requirements_met(), 'Minimum plugin version is ok' ); - $requirements->set_minimum_require_plugin_version( $minimum_plugin_version_fail ); - $this->assertFalse( $requirements->are_requirements_met(), - 'Failed as minimum required plugin version should be ' . $minimum_plugin_version_fail ); - - $this->expectOutputRegex( "/Plugin/" ); - $requirements->handle_render_notices_action(); - } - - /** - * @requires extension curl - */ - public function test_module_check() { - $requirements = $this->create_requirements_for_php_wp( - self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - $requirements->add_php_module_require( 'curl' ); - $this->assertTrue( $requirements->are_requirements_met(), 'Curl should exists' ); - - $this->expectOutputRegex( "/^$/" ); - $requirements->handle_render_notices_action(); - } - - public function test_plugin_check_with_multisite() { - $multisite = true; - $exising_plugin_name = 'WooCommerce'; - $exising_multisite_plugin_name = 'Multisite'; - $not_existing_plugin_name = 'Not exist'; - - WP_Mock::wpFunction( 'get_option' ) - ->withArgs( array( 'active_plugins', array() ) ) - ->andReturn( array( $exising_plugin_name ) ); - - WP_Mock::wpFunction( 'is_multisite' ) - ->andReturn( $multisite ); - - WP_Mock::wpFunction( 'get_site_option' ) - ->withArgs( array( 'active_sitewide_plugins', array() ) ) - ->andReturn( array( $exising_multisite_plugin_name ) ); - - - $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - $requirements->add_plugin_require( $exising_plugin_name ); - $this->assertTrue( $requirements->are_requirements_met(), 'Plugin should exists' ); - - $requirements->add_plugin_require( $exising_multisite_plugin_name ); - $this->assertTrue( $requirements->are_requirements_met(), 'Multisite plugin should exists' ); - - $requirements->add_plugin_require( $not_existing_plugin_name ); - $this->assertFalse( $requirements->are_requirements_met(), 'Plugin should not exists' ); - - $this->expectOutputRegex( "/$not_existing_plugin_name/" ); - $requirements->handle_render_notices_action(); - } - - /** - * @requires extension openssl - */ - public function test_existing_openssl_requirement() { - $open_ssl_always_valid = 1; - $open_ssl_always_not_valid = 0x900905000; // 9.9.6 - - $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - $this->assertTrue( $requirements->is_open_ssl_at_least( $open_ssl_always_valid ), - 'OpenSSL should have at least 0.1 version if exists' ); - - $this->assertFalse( $requirements->is_open_ssl_at_least( $open_ssl_always_not_valid ), - 'OpenSSL should fail for that high number' ); - - $requirements->set_min_openssl_require( $open_ssl_always_not_valid ); - - $this->assertFalse( $requirements->are_requirements_met(), - 'Requirement OpenSSL should fail for that high number' ); - - $this->expectOutputRegex( '/without OpenSSL module/' ); - $requirements->handle_render_notices_action(); - } - - public function test_deactivate_plugin_notice() { - $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_NOT_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - WP_Mock::expectActionAdded( WPDesk_Basic_Requirement_Checker::HOOK_ADMIN_NOTICES_ACTION, - array( $requirements, 'handle_render_notices_action' ) ); - - $this->assertFalse( $requirements->are_requirements_met() ); - $requirements->disable_plugin(); - $requirements->render_notices(); - - $this->expectOutputRegex( '/cannot run on PHP/' ); - $requirements->handle_render_notices_action(); - } - - public function test_add_plugin_repository_require_checks_for_activation_and_installs() { - $random_version = "1.0"; - $activated_plugin_name = 'WooCommerce'; - $not_activated_plugin_name = "some_other"; - $not_installed_plugin_name = "not_installed"; - $installed_plugin_names = array( $activated_plugin_name, $not_activated_plugin_name ); - - - WP_Mock::wpFunction( 'get_plugins' ) - ->andReturn( array_flip( $installed_plugin_names ) ); - - WP_Mock::wpFunction( 'get_option' ) - ->withArgs( array( 'active_plugins', array() ) ) - ->andReturn( array( $activated_plugin_name ) ); - - WP_Mock::passthruFunction( 'self_admin_url' ); - WP_Mock::passthruFunction( 'wp_kses' ); - WP_Mock::passthruFunction( 'wp_nonce_url' ); - WP_Mock::passthruFunction( 'wp_create_nonce' ); - WP_Mock::passthruFunction( 'admin_url' ); - - $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, - self::ALWAYS_VALID_WP_VERSION ); - - $requirements->add_plugin_repository_require( $activated_plugin_name, $random_version ); - $this->assertTrue( $requirements->are_requirements_met(), "Should be met for activated plugin" ); - - $requirements->add_plugin_repository_require( $not_activated_plugin_name, $random_version ); - $this->assertFalse( $requirements->are_requirements_met(), "Should NOT be met for only installed plugin" ); - - $this->expectOutputRegex( "/Activate $not_activated_plugin_name/" ); - $requirements->handle_render_notices_action(); - - $requirements->add_plugin_repository_require( $not_installed_plugin_name, $random_version ); - $this->expectOutputRegex( "/Install $not_installed_plugin_name/" ); - $this->assertFalse( $requirements->are_requirements_met(), - "Should NOT be met - uninstalled and unactive plugins are required" ); - $requirements->handle_render_notices_action(); - } -} + + + + /** + * @param string $php + * @param string $wp + * + * @return WPDesk_Basic_Requirement_Checker + */ + private function create_requirements_for_php_wp( $php, $wp ) { + return new WPDesk_Basic_Requirement_Checker( self::RANDOM_PLUGIN_FILE, self::RANDOM_PLUGIN_NAME, + self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp ); + } + + public function test_wp_version_check() { + $wp_version_fail = '4.1'; + + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $this->assertTrue( $requirements->are_requirements_met(), 'Should be ok because WP is OK and PHP is OK' ); + $requirements->set_min_wp_require( $wp_version_fail ); + $this->assertFalse( $requirements->are_requirements_met(), + 'Should fail because required WP should be at least ' . $wp_version_fail ); + + $this->expectOutputRegex( "/WordPress/" ); + $requirements->handle_render_notices_action(); + } + + /** + * @requires extension curl + */ + public function test_module_check() { + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $requirements->add_php_module_require( 'curl' ); + $this->assertTrue( $requirements->are_requirements_met(), 'Curl should exists' ); + + $this->expectOutputRegex( "/^$/" ); + $requirements->handle_render_notices_action(); + } + + public function test_plugin_check_with_multisite() { + $multisite = true; + $exising_plugin_name = 'WooCommerce'; + $exising_multisite_plugin_name = 'Multisite'; + $not_existing_plugin_name = 'Not exist'; + + WP_Mock::wpFunction( 'get_option' ) + ->withArgs( array( 'active_plugins', array() ) ) + ->andReturn( array( $exising_plugin_name ) ); + + WP_Mock::wpFunction( 'is_multisite' ) + ->andReturn( $multisite ); + + WP_Mock::wpFunction( 'get_site_option' ) + ->withArgs( array( 'active_sitewide_plugins', array() ) ) + ->andReturn( array( $exising_multisite_plugin_name ) ); + + + $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $requirements->add_plugin_require( $exising_plugin_name ); + $this->assertTrue( $requirements->are_requirements_met(), 'Plugin should exists' ); + + $requirements->add_plugin_require( $exising_multisite_plugin_name ); + $this->assertTrue( $requirements->are_requirements_met(), 'Multisite plugin should exists' ); + + $requirements->add_plugin_require( $not_existing_plugin_name ); + $this->assertFalse( $requirements->are_requirements_met(), 'Plugin should not exists' ); + + $this->expectOutputRegex( "/$not_existing_plugin_name/" ); + $requirements->handle_render_notices_action(); + } + + /** + * @requires extension openssl + */ + public function test_existing_openssl_requirement() { + $open_ssl_always_valid = 1; + $open_ssl_always_not_valid = 0x900905000; // 9.9.6 + + $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $this->assertTrue( $requirements->is_open_ssl_at_least( $open_ssl_always_valid ), + 'OpenSSL should have at least 0.1 version if exists' ); + + $this->assertFalse( $requirements->is_open_ssl_at_least( $open_ssl_always_not_valid ), + 'OpenSSL should fail for that high number' ); + + $requirements->set_min_openssl_require( $open_ssl_always_not_valid ); + + $this->assertFalse( $requirements->are_requirements_met(), + 'Requirement OpenSSL should fail for that high number' ); + + $this->expectOutputRegex( '/without OpenSSL module/' ); + $requirements->handle_render_notices_action(); + } + + public function test_deactivate_plugin_notice() { + $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_NOT_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + WP_Mock::expectActionAdded( WPDesk_Basic_Requirement_Checker::HOOK_ADMIN_NOTICES_ACTION, + array( $requirements, 'handle_render_notices_action' ) ); + + $this->assertFalse( $requirements->are_requirements_met() ); + $requirements->disable_plugin(); + $requirements->render_notices(); + + $this->expectOutputRegex( '/cannot run on PHP/' ); + $requirements->handle_render_notices_action(); + } + + public function test_add_plugin_repository_require_checks_for_activation_and_installs() { + $random_version = "1.0"; + $activated_plugin_name = 'WooCommerce'; + $not_activated_plugin_name = "some_other"; + $not_installed_plugin_name = "not_installed"; + $installed_plugin_names = array( $activated_plugin_name, $not_activated_plugin_name ); + + + WP_Mock::wpFunction( 'get_plugins' ) + ->andReturn( array_flip( $installed_plugin_names ) ); + + WP_Mock::wpFunction( 'get_option' ) + ->withArgs( array( 'active_plugins', array() ) ) + ->andReturn( array( $activated_plugin_name ) ); + + WP_Mock::passthruFunction( 'self_admin_url' ); + WP_Mock::passthruFunction( 'wp_kses' ); + WP_Mock::passthruFunction( 'wp_nonce_url' ); + WP_Mock::passthruFunction( 'wp_create_nonce' ); + WP_Mock::passthruFunction( 'admin_url' ); + + $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $requirements->add_plugin_repository_require( $activated_plugin_name, $random_version ); + $this->assertTrue( $requirements->are_requirements_met(), "Should be met for activated plugin" ); + + $requirements->add_plugin_require( $activated_plugin_name, $random_version ); + $this->assertTrue( $requirements->are_requirements_met(), 'Should be met for required plugins' ); + + $requirements->add_plugin_repository_require( $not_activated_plugin_name, $random_version ); + $this->assertFalse( $requirements->are_requirements_met(), "Should NOT be met for only installed plugin" ); + + $requirements->add_plugin_require( $not_activated_plugin_name, $random_version ); + $this->assertFalse( $requirements->are_requirements_met(), "Should NOT be met for only installed plugin" ); + + $this->expectOutputRegex( "/Activate $not_activated_plugin_name/" ); + $requirements->handle_render_notices_action(); + + $requirements->add_plugin_repository_require( $not_installed_plugin_name, $random_version ); + $this->expectOutputRegex( "/Install $not_installed_plugin_name/" ); + $this->assertFalse( $requirements->are_requirements_met(), + "Should NOT be met - uninstalled and unactive plugins are required" ); + $requirements->handle_render_notices_action(); + } + } \ No newline at end of file diff --git a/tests/unit/Test_Basic_Requirement_Checker_Factory.php b/tests/unit/Test_Basic_Requirement_Checker_Factory.php index adbf8d9578ddb9e8c825983c1e9a90a661e3389e..fc65e8f06b944c40fb51f91a08d641197eb0c3ce 100644 --- a/tests/unit/Test_Basic_Requirement_Checker_Factory.php +++ b/tests/unit/Test_Basic_Requirement_Checker_Factory.php @@ -1,52 +1,53 @@ <?php - - -class Test_Basic_Requirement_Checker_Factory extends PHPUnit\Framework\TestCase { - public function test_can_create_checker_withn_valid_requirements() { - $existing_locale = 'pl_PL'; - $requirements = array( - 'php' => '5.6', - 'wp' => '4.5', - 'plugins' => array( - array( - 'name' => 'woocommerce/woocommerce.php', - 'nice_name' => 'WooCommerce', + + + class Test_Basic_Requirement_Checker_Factory extends PHPUnit\Framework\TestCase { + public function test_can_create_checker_withn_valid_requirements() { + $existing_locale = 'pl_PL'; + $requirements = array( + 'php' => '5.6', + 'wp' => '4.5', + 'plugins' => array( + array( + 'name' => 'woocommerce/woocommerce.php', + 'nice_name' => 'WooCommerce', + 'version' => '1.0' + ), ), - ), - 'repo_plugins' => array( - array( - 'name' => 'flexible-checkout-fields/flexible-checkout-fields.php', - 'version' => '1.0', - 'nice_name' => 'Flexible Checkout Fields', + 'repo_plugins' => array( + array( + 'name' => 'flexible-checkout-fields/flexible-checkout-fields.php', + 'version' => '1.0', + 'nice_name' => 'Flexible Checkout Fields', + ), ), - ), - ); - - WP_Mock::wpFunction( 'get_locale' ) - ->andReturn( $existing_locale ); - WP_Mock::wpFunction( 'load_textdomain' ) - ->once(); // locale pl_PL exists so it should try to load it - - $factory = new WPDesk_Basic_Requirement_Checker_Factory(); - $checker = $factory->create_from_requirement_array( 'whatever', 'whatever', $requirements ); - - WP_Mock::wpFunction( 'get_plugins' ) - ->andReturn( array() ); - - WP_Mock::wpFunction( 'get_option' ) - ->withArgs( array( 'active_plugins', array() ) ) - ->andReturn( array() ); - - WP_Mock::passthruFunction( 'self_admin_url' ); - WP_Mock::passthruFunction( 'wp_kses' ); - WP_Mock::passthruFunction( 'wp_nonce_url' ); - WP_Mock::passthruFunction( 'wp_create_nonce' ); - WP_Mock::passthruFunction( 'admin_url' ); - - $this->assertFalse( $checker->are_requirements_met(), '2 plugins required and there should be none activated' ); - - $this->expectOutputRegex( '/Flexible Checkout Fields/' ); - $this->expectOutputRegex( '/WooCommerce/' ); - $checker->handle_render_notices_action(); - } -} + ); + + WP_Mock::wpFunction( 'get_locale' ) + ->andReturn( $existing_locale ); + WP_Mock::wpFunction( 'load_textdomain' ) + ->once(); // locale pl_PL exists so it should try to load it + + $factory = new WPDesk_Basic_Requirement_Checker_Factory(); + $checker = $factory->create_from_requirement_array( 'whatever', 'whatever', $requirements ); + + WP_Mock::wpFunction( 'get_plugins' ) + ->andReturn( array() ); + + WP_Mock::wpFunction( 'get_option' ) + ->withArgs( array( 'active_plugins', array() ) ) + ->andReturn( array() ); + + WP_Mock::passthruFunction( 'self_admin_url' ); + WP_Mock::passthruFunction( 'wp_kses' ); + WP_Mock::passthruFunction( 'wp_nonce_url' ); + WP_Mock::passthruFunction( 'wp_create_nonce' ); + WP_Mock::passthruFunction( 'admin_url' ); + + $this->assertFalse( $checker->are_requirements_met(), '2 plugins required and there should be none activated' ); + + $this->expectOutputRegex( '/Flexible Checkout Fields/' ); + $this->expectOutputRegex( '/WooCommerce/' ); + $checker->handle_render_notices_action(); + } + } \ No newline at end of file