-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestNonces.php
89 lines (56 loc) · 1.92 KB
/
TestNonces.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php // phpcs:ignore WordPress.Files.FileName
/**
* Rabbit nonces test.
*
* @package rabbit-foundation
* @author Sematico LTD <[email protected]>
* @copyright 2020 Sematico LTD
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later
* @link https://sematico.com
*/
namespace Rabbit\Tests;
use Rabbit\Nonces\Nonce;
use Rabbit\Nonces\NonceFactory;
class TestNonces extends \WP_UnitTestCase {
public $slug = 'nonceSlug';
public function testCreateNonceInstance() {
$nonce = new Nonce( $this->slug );
$this->assertEquals( $nonce->getKey(), "_{$this->slug}-nonce" );
}
public function testGenerateNonceUrl() {
$nonce = new Nonce( $this->slug );
$this->assertTrue( strpos( $nonce->url( 'http://example.com' ), "?_{$this->slug}-nonce" ) !== false );
}
public function testGenerateNonceField() {
$nonce = new Nonce( $this->slug );
$expected = '<input type="hidden" id="_' . $this->slug . '-nonce" name="_' . $this->slug . '-nonce" value="' . $nonce->make() . '" /><input type="hidden" name="_wp_http_referer" value="" />';
$this->assertEquals( $nonce->render(), $expected );
}
public function testValidateNonce() {
$user = $this->factory->user->create_and_get(
array(
'user_login' => 'waldo',
'user_pass' => null,
'role' => 'subscriber',
)
);
$this->assertTrue( 0 !== $user->ID );
wp_set_current_user( $user->ID );
$nonce = new Nonce( $this->slug );
$_POST[ $nonce->getKey() ] = $nonce->make();
$this->assertTrue( $nonce->check( $_POST[ $nonce->getKey() ] ) );
}
public function testFactoryValidation() {
$user = $this->factory->user->create_and_get(
array(
'user_login' => 'waldo',
'user_pass' => null,
'role' => 'subscriber',
)
);
wp_set_current_user( $user->ID );
$nonce = new Nonce( $this->slug );
$_POST[ $nonce->getKey() ] = $nonce->make();
$this->assertTrue( NonceFactory::verify( $this->slug ) );
}
}