1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * A simple test for testing added action to wp_verify_nonce |
---|
5 | * |
---|
6 | */ |
---|
7 | class Tests_wp_verify_nonce extends WP_UnitTestCase { |
---|
8 | /** |
---|
9 | * counter |
---|
10 | * @var int |
---|
11 | */ |
---|
12 | protected $hook_called_count = 0; |
---|
13 | |
---|
14 | /** |
---|
15 | * action hook |
---|
16 | */ |
---|
17 | protected $failure_hook = 'wp_verify_nonce_failed'; |
---|
18 | |
---|
19 | /** |
---|
20 | * test action name for nonce generation |
---|
21 | */ |
---|
22 | protected $test_action = 'wp_nonce_test'; |
---|
23 | |
---|
24 | /** |
---|
25 | * Simple method to count our action calls |
---|
26 | */ |
---|
27 | function _helper_count_hook($uid, $action) { |
---|
28 | $this->hook_called_count++; |
---|
29 | $this->assertEquals( 0, $uid); |
---|
30 | $this->assertEquals( $this->test_action, $action ); |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Set up the test fixture, register our wp_verify_once_failed action |
---|
35 | */ |
---|
36 | public function setUp() { |
---|
37 | parent::setUp(); |
---|
38 | add_action( $this->failure_hook, array( $this, '_helper_count_hook' ), 10, 2 ); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Tear down the test fixture. |
---|
43 | * Reset the current user |
---|
44 | */ |
---|
45 | public function tearDown() { |
---|
46 | parent::tearDown(); |
---|
47 | remove_action( $this->failure_hook, array( $this, '_helper_count_hook' ) ); |
---|
48 | } |
---|
49 | |
---|
50 | function test_wp_nonce_verify_failed() { |
---|
51 | $fake_nonce = substr( md5( uniqid() ), 0, 10 ); |
---|
52 | wp_verify_nonce( $fake_nonce, $this->test_action ); |
---|
53 | $this->assertEquals( 1, $this->hook_called_count ); |
---|
54 | } |
---|
55 | |
---|
56 | function test_wp_nonce_verify_success() { |
---|
57 | $created_nonce = wp_create_nonce( $this->test_action ); |
---|
58 | wp_verify_nonce( $created_nonce, $this->test_action ); |
---|
59 | $this->assertEquals( 0, $this->hook_called_count ); |
---|
60 | } |
---|
61 | } |
---|