| 1 |
<?php |
| 2 |
/** |
| 3 |
* Test_Set_Cbid_Ability class. |
| 4 |
* |
| 5 |
* @package cookiebot |
| 6 |
* @subpackage tests |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace cybot\cookiebot\tests\integration\abilities; |
| 10 |
|
| 11 |
use cybot\cookiebot\abilities\Ability_Audit_Logger; |
| 12 |
use cybot\cookiebot\abilities\Set_Cbid_Ability; |
| 13 |
use WP_UnitTestCase; |
| 14 |
|
| 15 |
/** |
| 16 |
* Tests for Set_Cbid_Ability. |
| 17 |
* |
| 18 |
* @since 4.8.0 |
| 19 |
*/ |
| 20 |
class Test_Set_Cbid_Ability extends WP_UnitTestCase { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var Ability_Audit_Logger |
| 24 |
*/ |
| 25 |
private $logger; |
| 26 |
|
| 27 |
public function set_up() { |
| 28 |
parent::set_up(); |
| 29 |
$this->logger = new Ability_Audit_Logger(); |
| 30 |
delete_option( 'cookiebot-ai-action-log' ); |
| 31 |
update_option( 'cookiebot-cbid', '11111111-1111-1111-1111-111111111111' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function test_get_name() { |
| 35 |
$this->assertSame( 'cookiebot/set-cbid', ( new Set_Cbid_Ability( $this->logger ) )->get_name() ); |
| 36 |
} |
| 37 |
|
| 38 |
public function test_updates_option_on_valid_cbid() { |
| 39 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 40 |
$result = call_user_func( $args['execute_callback'], array( 'cbid' => 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF' ) ); |
| 41 |
$this->assertTrue( $result['success'] ); |
| 42 |
$this->assertSame( 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF', get_option( 'cookiebot-cbid' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
public function test_returns_cbid_set_flags_not_raw_values() { |
| 46 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 47 |
$result = call_user_func( $args['execute_callback'], array( 'cbid' => 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF' ) ); |
| 48 |
// Response must use boolean flags, never the raw CBID value (security: CBID is sensitive). |
| 49 |
$this->assertTrue( $result['old_cbid_set'] ); |
| 50 |
$this->assertTrue( $result['new_cbid_set'] ); |
| 51 |
$this->assertArrayNotHasKey( 'old_cbid', $result ); |
| 52 |
$this->assertArrayNotHasKey( 'new_cbid', $result ); |
| 53 |
} |
| 54 |
|
| 55 |
public function test_old_cbid_set_false_when_previously_unset() { |
| 56 |
update_option( 'cookiebot-cbid', '' ); |
| 57 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 58 |
$result = call_user_func( $args['execute_callback'], array( 'cbid' => 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF' ) ); |
| 59 |
$this->assertFalse( $result['old_cbid_set'] ); |
| 60 |
$this->assertTrue( $result['new_cbid_set'] ); |
| 61 |
} |
| 62 |
|
| 63 |
public function test_writes_audit_log_on_success() { |
| 64 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 65 |
call_user_func( $args['execute_callback'], array( 'cbid' => 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF' ) ); |
| 66 |
$log = get_option( 'cookiebot-ai-action-log', array() ); |
| 67 |
$this->assertCount( 1, $log ); |
| 68 |
$this->assertSame( 'cookiebot/set-cbid', $log[0]['ability'] ); |
| 69 |
// Audit log must NOT store raw CBID values. |
| 70 |
$this->assertNotContains( 'AABBCCDD-1111-2222-3333-AABBCCDDEEFF', $log[0] ); |
| 71 |
$this->assertNotContains( '11111111-1111-1111-1111-111111111111', $log[0] ); |
| 72 |
} |
| 73 |
|
| 74 |
public function test_returns_wp_error_on_invalid_uuid() { |
| 75 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 76 |
$result = call_user_func( $args['execute_callback'], array( 'cbid' => 'not-a-uuid' ) ); |
| 77 |
$this->assertInstanceOf( 'WP_Error', $result ); |
| 78 |
$this->assertSame( 'cookiebot_invalid_cbid', $result->get_error_code() ); |
| 79 |
} |
| 80 |
|
| 81 |
public function test_returns_wp_error_on_empty_cbid() { |
| 82 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 83 |
$result = call_user_func( $args['execute_callback'], array( 'cbid' => '' ) ); |
| 84 |
$this->assertInstanceOf( 'WP_Error', $result ); |
| 85 |
$this->assertSame( 'cookiebot_invalid_cbid', $result->get_error_code() ); |
| 86 |
} |
| 87 |
|
| 88 |
public function test_no_log_written_on_error() { |
| 89 |
$args = ( new Set_Cbid_Ability( $this->logger ) )->get_args(); |
| 90 |
call_user_func( $args['execute_callback'], array( 'cbid' => 'bad' ) ); |
| 91 |
$this->assertEmpty( get_option( 'cookiebot-ai-action-log', array() ) ); |
| 92 |
} |
| 93 |
} |
| 94 |
|