| 1 |
<?php |
| 2 |
// tests/integration/abilities/Test_Install_Ppg_Ability.php |
| 3 |
|
| 4 |
namespace cybot\cookiebot\tests\integration\abilities; |
| 5 |
|
| 6 |
use cybot\cookiebot\abilities\Ability_Audit_Logger; |
| 7 |
use cybot\cookiebot\abilities\Install_Ppg_Ability; |
| 8 |
use WP_UnitTestCase; |
| 9 |
|
| 10 |
class Test_Install_Ppg_Ability extends WP_UnitTestCase { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Ability_Audit_Logger |
| 14 |
*/ |
| 15 |
private $logger; |
| 16 |
|
| 17 |
public function set_up() { |
| 18 |
parent::set_up(); |
| 19 |
$this->logger = new Ability_Audit_Logger(); |
| 20 |
delete_option( 'cookiebot-ai-action-log' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function tear_down() { |
| 24 |
remove_all_filters( 'pre_option_active_plugins' ); |
| 25 |
remove_all_filters( 'plugins_api' ); |
| 26 |
remove_all_filters( 'all_plugins' ); |
| 27 |
parent::tear_down(); |
| 28 |
} |
| 29 |
|
| 30 |
public function test_get_name() { |
| 31 |
$this->assertSame( 'cookiebot/install-ppg', ( new Install_Ppg_Ability( $this->logger ) )->get_name() ); |
| 32 |
} |
| 33 |
|
| 34 |
public function test_returns_was_already_active_when_plugin_active() { |
| 35 |
add_filter( 'pre_option_active_plugins', function() { |
| 36 |
return array( 'privacy-policy-usercentrics/privacy-policy-usercentrics.php' ); |
| 37 |
} ); |
| 38 |
$args = ( new Install_Ppg_Ability( $this->logger ) )->get_args(); |
| 39 |
$result = call_user_func( $args['execute_callback'] ); |
| 40 |
$this->assertTrue( $result['was_already_active'] ); |
| 41 |
$this->assertTrue( $result['success'] ); |
| 42 |
$this->assertArrayHasKey( 'admin_url', $result ); |
| 43 |
} |
| 44 |
|
| 45 |
public function test_writes_audit_log_when_already_active() { |
| 46 |
add_filter( 'pre_option_active_plugins', function() { |
| 47 |
return array( 'privacy-policy-usercentrics/privacy-policy-usercentrics.php' ); |
| 48 |
} ); |
| 49 |
$args = ( new Install_Ppg_Ability( $this->logger ) )->get_args(); |
| 50 |
call_user_func( $args['execute_callback'] ); |
| 51 |
$log = get_option( 'cookiebot-ai-action-log', array() ); |
| 52 |
$this->assertCount( 1, $log ); |
| 53 |
$this->assertSame( 'cookiebot/install-ppg', $log[0]['ability'] ); |
| 54 |
} |
| 55 |
|
| 56 |
public function test_returns_wp_error_when_plugins_api_fails() { |
| 57 |
// Force plugins cache to empty so get_plugins() skips the filesystem scan |
| 58 |
// and returns [] without finding privacy-policy-usercentrics. |
| 59 |
wp_cache_set( 'plugins', array( '' => array() ), 'plugins' ); |
| 60 |
// Ensure plugin is not active. |
| 61 |
add_filter( 'pre_option_active_plugins', function() { |
| 62 |
return array(); |
| 63 |
} ); |
| 64 |
add_filter( 'plugins_api', function() { |
| 65 |
return new \WP_Error( 'api_error', 'Connection failed.' ); |
| 66 |
} ); |
| 67 |
$args = ( new Install_Ppg_Ability( $this->logger ) )->get_args(); |
| 68 |
$result = call_user_func( $args['execute_callback'] ); |
| 69 |
$this->assertInstanceOf( 'WP_Error', $result ); |
| 70 |
$this->assertSame( 'cookiebot_ppg_install_failed', $result->get_error_code() ); |
| 71 |
} |
| 72 |
|
| 73 |
public function test_show_in_rest_true() { |
| 74 |
$args = ( new Install_Ppg_Ability( $this->logger ) )->get_args(); |
| 75 |
$this->assertTrue( $args['meta']['show_in_rest'] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|