woocommerce/tests/unit-tests/util/plugin-updates.php

271 lines
9.8 KiB
PHP
Raw Normal View History

2017-08-07 21:31:58 +00:00
<?php
/**
* Class WC_Plugin_Updates.
* @package WooCommerce\Tests\Util
* @since 3.2.0
*/
class WC_Tests_Plugin_Updates extends WC_Unit_Test_Case {
/** @var WC_Plugin_Updates instance */
protected $updates;
/** @var array of plugin data for testing*/
protected $plugins = array();
/**
* Setup test.
*
* @since 3.2.0
*/
public function setUp() {
parent::setUp();
if ( ! class_exists( 'WC_Plugin_Updates' ) ) {
$bootstrap = WC_Unit_Tests_Bootstrap::instance();
include_once $bootstrap->plugin_dir . '/includes/admin/plugin-updates/class-wc-plugin-updates.php';
2017-08-07 21:31:58 +00:00
}
$this->updates = new WC_Plugin_Updates();
2017-08-07 21:31:58 +00:00
$this->plugins = array();
add_filter( 'woocommerce_get_plugins_with_header', array( $this, 'populate_untested_plugins' ), 10, 2 );
}
/**
* Allow this test suite to easily define plugin results to test for the version tested header.
*
* @param array $plugins array of plugin data in same format as get_plugins.
2017-08-07 21:31:58 +00:00
* @param string $header plugin header results matched on.
* @return array modified $plugins.
* @since 3.2.0
*/
public function populate_untested_plugins( $plugins, $header ) {
2017-08-22 12:24:29 +00:00
if ( WC_Plugin_Updates::VERSION_TESTED_HEADER === $header && ! empty( $this->plugins ) ) {
2017-08-07 21:31:58 +00:00
$plugins = $this->plugins;
update_option( 'active_plugins', array_keys( $this->plugins ) );
}
return $plugins;
}
/**
* Test WC_Plugin_Updates::get_untested_plugins with a variety of tested plugins for major release.
*
* @since 3.2.0
*/
public function test_get_untested_plugins_major_good() {
$release = 'major';
$this->plugins = array(
'test/test.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.0.0',
2017-08-07 21:31:58 +00:00
),
'test2/test2.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 2',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '5.0',
2017-08-07 21:31:58 +00:00
),
'test3/test3.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 3',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.1.0',
2017-08-07 21:31:58 +00:00
),
'test4/test4.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 4',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.0.1',
2017-08-07 21:31:58 +00:00
),
);
$new_version = '4.0.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-08 17:31:06 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
$new_version = '3.9.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-08 17:31:06 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
$new_version = '4.3.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-08 17:31:06 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
$new_version = '4.0.2';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-08 17:31:06 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
}
/**
* Test WC_Plugin_Updates::get_untested_plugins with a variety of untested plugins for major release.
*
* @since 3.2.0
*/
public function test_get_untested_plugins_major_bad() {
$release = 'major';
$this->plugins = array(
'test/test.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '3.0.0',
2017-08-07 21:31:58 +00:00
),
'test2/test2.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 2',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '3.9.9',
2017-08-07 21:31:58 +00:00
),
'test3/test3.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 3',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '3.0',
2017-08-07 21:31:58 +00:00
),
);
$plugin_keys = array_keys( $this->plugins );
2017-08-22 22:03:54 +00:00
2017-08-07 21:31:58 +00:00
$new_version = '4.0.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertEquals( $plugin_keys, array_intersect( $plugin_keys, array_keys( $untested ) ) );
2017-08-07 21:31:58 +00:00
$new_version = '4.3.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertEquals( $plugin_keys, array_intersect( $plugin_keys, array_keys( $untested ) ) );
2017-08-07 21:31:58 +00:00
$new_version = '4.0.2';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertEquals( $plugin_keys, array_intersect( $plugin_keys, array_keys( $untested ) ) );
2017-08-07 21:31:58 +00:00
}
/**
* Test WC_Plugin_Updates::get_untested_plugins with a variety of tested plugins for minor release.
*
* @since 3.2.0
*/
public function test_get_untested_plugins_minor_good() {
$release = 'minor';
$this->plugins = array(
'test/test.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.1.0',
2017-08-07 21:31:58 +00:00
),
'test2/test2.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 2',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '5.0.0',
2017-08-07 21:31:58 +00:00
),
'test3/test3.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 3',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.1.1',
2017-08-07 21:31:58 +00:00
),
'test4/test4.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 4',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.2.1',
2017-08-07 21:31:58 +00:00
),
);
$new_version = '4.1.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
$new_version = '4.2.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertArrayHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
$new_version = '4.1.5';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
}
/**
* Test WC_Plugin_Updates::get_untested_plugins with a variety of untested plugins for minor release.
*
* @since 3.2.0
*/
public function test_get_untested_plugins_minor_bad() {
$release = 'minor';
$this->plugins = array(
'test/test.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.1.0',
2017-08-07 21:31:58 +00:00
),
'test2/test2.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 2',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '3.9.0',
2017-08-07 21:31:58 +00:00
),
'test3/test3.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 3',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4.2',
2017-08-07 21:31:58 +00:00
),
);
$plugin_keys = array_keys( $this->plugins );
2017-08-22 22:03:54 +00:00
2017-08-07 21:31:58 +00:00
$new_version = '4.3.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertEquals( $plugin_keys, array_intersect( $plugin_keys, array_keys( $untested ) ) );
2017-08-07 21:31:58 +00:00
$new_version = '4.3.1';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertEquals( $plugin_keys, array_intersect( $plugin_keys, array_keys( $untested ) ) );
2017-08-07 21:31:58 +00:00
$new_version = '4.1.0';
2017-08-22 22:03:54 +00:00
$this->assertArrayHasKey( 'test2/test2.php', $this->updates->get_untested_plugins( $new_version, $release ) );
2017-08-07 21:31:58 +00:00
$new_version = '4.1.5';
2017-08-22 22:03:54 +00:00
$this->assertArrayHasKey( 'test2/test2.php', $this->updates->get_untested_plugins( $new_version, $release ) );
2017-08-07 21:31:58 +00:00
}
/**
* Test WC_Plugin_Updates::get_untested_plugins with a variety of incorrect values for the header.
* Generally headers with incorrectly formatted values should be ignored, but mostly errors should be avoided.
*
* @since 3.2.0
*/
public function test_get_untested_plugins_malformed() {
$release = 'minor';
$this->plugins = array(
'test/test.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => '4',
2017-08-07 21:31:58 +00:00
),
'test2/test2.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 2',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => 'Latest release',
2017-08-07 21:31:58 +00:00
),
'test3/test3.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 3',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => 'WC 3.0.0',
2017-08-07 21:31:58 +00:00
),
'test4/test4.php' => array(
2017-08-22 22:03:54 +00:00
'Name' => 'Test plugin 4',
2017-08-22 12:24:29 +00:00
WC_Plugin_Updates::VERSION_TESTED_HEADER => ' ',
2017-08-07 21:31:58 +00:00
),
);
$release = 'major';
2017-08-07 21:31:58 +00:00
$new_version = '5.0.0';
2017-08-22 22:03:54 +00:00
$this->assertArrayHasKey( 'test/test.php', $this->updates->get_untested_plugins( $new_version, $release ) );
2017-08-07 21:31:58 +00:00
$release = 'minor';
2017-08-07 21:31:58 +00:00
$new_version = '4.1.0';
$untested = $this->updates->get_untested_plugins( $new_version, $release );
2017-08-22 22:03:54 +00:00
$this->assertArrayNotHasKey( 'test/test.php', $untested );
$this->assertArrayNotHasKey( 'test2/test2.php', $untested );
$this->assertArrayNotHasKey( 'test3/test3.php', $untested );
$this->assertArrayNotHasKey( 'test4/test4.php', $untested );
2017-08-07 21:31:58 +00:00
}
}