| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tests for Cookiebot_CLI_Command. |
| 4 |
* |
| 5 |
* @package cookiebot |
| 6 |
* @subpackage tests |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace cybot\cookiebot\tests\integration\cli; |
| 10 |
|
| 11 |
use cybot\cookiebot\cli\Cookiebot_CLI_Command; |
| 12 |
use cybot\cookiebot\cli\Output_Adapter; |
| 13 |
use WP_UnitTestCase; |
| 14 |
|
| 15 |
/** |
| 16 |
* Fake output adapter that records all output calls for assertions. |
| 17 |
*/ |
| 18 |
class Fake_Output implements Output_Adapter { |
| 19 |
public $successes = array(); |
| 20 |
public $errors = array(); |
| 21 |
public $items = array(); |
| 22 |
|
| 23 |
public function success( $message ) { |
| 24 |
$this->successes[] = $message; |
| 25 |
} |
| 26 |
|
| 27 |
public function error( $message ) { |
| 28 |
$this->errors[] = $message; |
| 29 |
} |
| 30 |
|
| 31 |
public function format_items( $items, $fields, $format ) { |
| 32 |
$this->items[] = array( |
| 33 |
'items' => $items, |
| 34 |
'fields' => $fields, |
| 35 |
'format' => $format, |
| 36 |
); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Integration tests for Cookiebot_CLI_Command. |
| 42 |
* |
| 43 |
* @since 4.8.0 |
| 44 |
*/ |
| 45 |
class Test_Cookiebot_CLI_Command extends WP_UnitTestCase { |
| 46 |
|
| 47 |
/** |
| 48 |
* Set up test fixtures. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function set_up() { |
| 53 |
parent::set_up(); |
| 54 |
// Run as an administrator so that manage_options passes in execute(). |
| 55 |
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
| 56 |
wp_set_current_user( $user_id ); |
| 57 |
update_option( 'cookiebot-cbid', 'aabbccdd-1111-2222-3333-aabbccddeeff' ); |
| 58 |
update_option( 'cookiebot-gcm', '1' ); |
| 59 |
update_option( 'cookiebot-cookie-blocking-mode', 'auto' ); |
| 60 |
update_option( 'cookiebot-banner-enabled', '1' ); |
| 61 |
} |
| 62 |
|
| 63 |
public function test_status_calls_get_status_ability_and_outputs_one_row() { |
| 64 |
$output = new Fake_Output(); |
| 65 |
$command = new Cookiebot_CLI_Command( $output ); |
| 66 |
|
| 67 |
$command->status( array(), array( 'format' => 'json' ) ); |
| 68 |
|
| 69 |
$this->assertCount( 1, $output->items ); |
| 70 |
$this->assertCount( 1, $output->items[0]['items'] ); |
| 71 |
$row = $output->items[0]['items'][0]; |
| 72 |
$this->assertArrayHasKey( 'cbid_set', $row ); |
| 73 |
$this->assertArrayHasKey( 'gcm_enabled', $row ); |
| 74 |
$this->assertArrayHasKey( 'plugin_version', $row ); |
| 75 |
$this->assertEmpty( $output->errors ); |
| 76 |
} |
| 77 |
|
| 78 |
public function test_set_cbid_with_invalid_uuid_routes_to_error() { |
| 79 |
$output = new Fake_Output(); |
| 80 |
$command = new Cookiebot_CLI_Command( $output ); |
| 81 |
|
| 82 |
$command->set_cbid( array( 'not-a-uuid' ), array() ); |
| 83 |
|
| 84 |
$this->assertCount( 1, $output->errors ); |
| 85 |
$this->assertEmpty( $output->successes ); |
| 86 |
} |
| 87 |
|
| 88 |
public function test_set_cbid_with_valid_uuid_calls_success() { |
| 89 |
$output = new Fake_Output(); |
| 90 |
$command = new Cookiebot_CLI_Command( $output ); |
| 91 |
|
| 92 |
$command->set_cbid( array( '12345678-1234-1234-1234-123456789abc' ), array() ); |
| 93 |
|
| 94 |
$this->assertCount( 1, $output->successes ); |
| 95 |
// The success message must NOT contain the CBID value (security: CBID is sensitive). |
| 96 |
$this->assertStringNotContainsString( '12345678', $output->successes[0] ); |
| 97 |
$this->assertStringContainsString( 'CBID updated', $output->successes[0] ); |
| 98 |
$this->assertEmpty( $output->errors ); |
| 99 |
} |
| 100 |
|
| 101 |
public function test_toggle_gcm_without_flag_routes_to_error() { |
| 102 |
$output = new Fake_Output(); |
| 103 |
$command = new Cookiebot_CLI_Command( $output ); |
| 104 |
|
| 105 |
$command->toggle_gcm( array(), array() ); |
| 106 |
|
| 107 |
$this->assertCount( 1, $output->errors ); |
| 108 |
} |
| 109 |
} |
| 110 |
|