Completed
Push — kraftbj-patch-2 ( f1a4bb...ba1272 )
by
unknown
25:04 queued 15:38
created

Test_Nonce_Handler::test_delete()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 1
nop 0
dl 0
loc 46
rs 8.2448
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
/**
4
 * The nonce handler tests.
5
 *
6
 * @package automattic/jetpack-connection
7
 */
8
9
namespace Automattic\Jetpack\Connection;
10
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * The nonce handler tests.
15
 */
16
class Test_Nonce_Handler extends TestCase {
17
18
	/**
19
	 * The nonce timestamp.
20
	 */
21
	const TIMESTAMP = '1598639691';
22
23
	/**
24
	 * The nonce.
25
	 */
26
	const NONCE = 'rAnDoM';
27
28
	/**
29
	 * Reset the environment after each test.
30
	 *
31
	 * @after
32
	 */
33
	protected function tear_down() {
34
		Nonce_Handler::invalidate_request_nonces();
35
	}
36
37
	/**
38
	 * Testing the nonce adding functionality.
39
	 */
40
	public function test_add() {
41
		// Confirm that the nonce gets added.
42
		self::assertTrue( ( new Nonce_Handler() )->add( static::TIMESTAMP, static::NONCE ) );
43
44
		// Confirm that the nonce is loaded from cache and still valid during the request.
45
		self::assertTrue( ( new Nonce_Handler() )->add( static::TIMESTAMP, static::NONCE ) );
46
	}
47
48
	/**
49
	 * Testing the cleanup hook scheduling.
50
	 */
51
	public function test_schedule() {
52
		global $wp_filter;
53
54
		$is_callback_missing = empty( $wp_filter['jetpack_clean_nonces']->callbacks[10][ Nonce_Handler::class . '::clean_scheduled' ] );
55
		$is_schedule_missing = ! wp_next_scheduled( 'jetpack_clean_nonces' );
56
57
		( new Nonce_Handler() )->init_schedule();
58
		$is_callback_placed = ! empty( $wp_filter['jetpack_clean_nonces']->callbacks[10][ Nonce_Handler::class . '::clean_scheduled' ] );
59
		$is_scheduled       = (bool) wp_next_scheduled( 'jetpack_clean_nonces' );
60
61
		self::assertTrue( $is_callback_missing, 'Cleanup callback is in place, it should not exist' );
62
		self::assertTrue( $is_schedule_missing, 'Schedule is in place, it should not exist' );
63
64
		self::assertTrue( $is_callback_placed, 'Cleanup callback is missing, it should have been added' );
65
		self::assertTrue( $is_scheduled, 'Schedule is missing, it should have been added' );
66
	}
67
68
	/**
69
	 * Trying to add an existing nonce, and making sure it's considered invalid (returns false).
70
	 */
71
	public function test_add_existing() {
72
		$query_filter_run = false;
73
74
		$query_filter = function ( $result, $query ) use ( &$query_filter_run ) {
75
			if ( ! $query_filter_run && false !== strpos( $query, 'jetpack_nonce_' ) ) {
76
				global $wpdb;
77
78
				$query_filter_run = true;
79
				$nonce_name       = 'jetpack_nonce_' . static::TIMESTAMP . '_' . static::NONCE;
80
				$this->assertEquals( "SELECT 1 FROM `{$wpdb->options}` WHERE option_name = '{$nonce_name}'", $query );
81
82
				return array( (object) array( 1 => '1' ) );
83
			}
84
85
			return $result;
86
		};
87
88
		add_filter( 'wordbless_wpdb_query_results', $query_filter, 10, 2 );
89
90
		$result = ( new Nonce_Handler() )->add( static::TIMESTAMP, static::NONCE );
91
92
		remove_filter( 'wordbless_wpdb_query_results', $query_filter );
93
94
		self::assertFalse( $result );
95
		self::assertTrue( $query_filter_run, "The SQL query assertions haven't run." );
96
	}
97
98
	/**
99
	 * Testing the runtime nonce cleanup functionality.
100
	 */
101
	public function test_delete() {
102
		$nonce_ids        = array( 1111, 2222 );
103
		$limit            = 42;
104
		$cutoff_timestamp = static::TIMESTAMP;
105
106
		$query_filter_select_run = false;
107
		$query_filter_delete_run = false;
108
109
		$query_filter_select = function ( $result, $query ) use ( &$query_filter_select_run, $nonce_ids, $limit, $cutoff_timestamp ) {
110
			if ( ! $query_filter_select_run && 0 === strpos( $query, 'SELECT ' ) && false !== strpos( $query, 'jetpack_nonce_' ) ) {
111
				global $wpdb;
112
113
				$query_filter_select_run = true;
114
				self::assertEquals( "SELECT option_id FROM `{$wpdb->options}` WHERE `option_name` >= 'jetpack_nonce_' AND `option_name` < 'jetpack_nonce_{$cutoff_timestamp}' LIMIT {$limit}", $query );
115
116
				return array(
117
					(object) array( 'option_id' => $nonce_ids[0] ),
118
					(object) array( 'option_id' => $nonce_ids[1] ),
119
				);
120
			}
121
122
			return $result;
123
		};
124
125
		$query_filter_delete = function ( $result, $query ) use ( &$query_filter_delete_run, $nonce_ids ) {
126
			if ( ! $query_filter_delete_run && 0 === strpos( $query, 'DELETE ' ) && false !== strpos( $query, 'option_id' ) ) {
127
				global $wpdb;
128
129
				$query_filter_delete_run = true;
130
				self::assertEquals( "DELETE FROM `{$wpdb->options}` WHERE `option_id` IN ( " . implode( ', ', $nonce_ids ) . ' )', $query );
131
			}
132
133
			return $result;
134
		};
135
136
		add_filter( 'wordbless_wpdb_query_results', $query_filter_select, 10, 2 );
137
		add_filter( 'wordbless_wpdb_query_results', $query_filter_delete, 10, 2 );
138
139
		( new Nonce_Handler() )->delete( $limit, $cutoff_timestamp );
140
141
		remove_filter( 'wordbless_wpdb_query_results', $query_filter_select );
142
		remove_filter( 'wordbless_wpdb_query_results', $query_filter_delete );
143
144
		self::assertTrue( $query_filter_select_run, "The SQL query assertions haven't run." );
145
		self::assertTrue( $query_filter_delete_run, "The SQL query assertions haven't run." );
146
	}
147
148
	/**
149
	 * Testing the shutdown cleanup hook.
150
	 */
151
	public function test_reschedule() {
152
		$handler = new Nonce_Handler();
153
154
		$handler->init_schedule();
155
		$scheduled_timestamp = wp_next_scheduled( 'jetpack_clean_nonces' );
156
157
		sleep( 1 );
158
159
		$handler->reschedule();
160
		$rescheduled_timestamp = wp_next_scheduled( 'jetpack_clean_nonces' );
161
162
		self::assertGreaterThan( $scheduled_timestamp, $rescheduled_timestamp );
163
	}
164
}
165