/
class-test-admin-notices.php
127 lines (99 loc) · 2.75 KB
/
class-test-admin-notices.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Test_Admin_Notices class file
*
* @package cyr-to-lat
*/
namespace Cyr_To_Lat;
use ReflectionClass;
use ReflectionException;
use WP_Mock;
/**
* Class Test_Admin_Notices
*
* @group admin-notices
*/
class Test_Admin_Notices extends Cyr_To_Lat_TestCase {
/**
* Test constructor
*
* @throws ReflectionException Reflection Exception.
* @noinspection NullPointerExceptionInspection
*/
public function test_constructor() {
$classname = Admin_Notices::class;
// Get mock, without the constructor being called.
$mock = $this->getMockBuilder( $classname )->disableOriginalConstructor()->getMock();
// Set expectations for constructor calls.
WP_Mock::expectActionAdded( 'admin_notices', [ $mock, 'show_notices' ] );
// Now call the constructor.
$reflected_class = new ReflectionClass( $classname );
$constructor = $reflected_class->getConstructor();
$constructor->invoke( $mock );
}
/**
* Test add_notice() and show_notices()
*/
public function test_add_and_show_notices() {
$expected = ' <div class="notice">
<p>
<strong>
First message </strong>
</p>
</div>
<div class="error">
<p>
<strong>
Second message </strong>
</p>
</div>
';
$subject = new Admin_Notices();
WP_Mock::passthruFunction( 'wp_kses_post' );
ob_start();
$subject->show_notices();
$result = ob_get_clean();
self::assertEmpty( $result );
$subject->add_notice( 'First message' );
$subject->add_notice( 'Second message', 'error' );
ob_start();
$subject->show_notices();
$result = ob_get_clean();
self::assertSame( $expected, $result );
}
/**
* Test add_notice() and show_notices() when page is not allowed
*/
public function test_show_notices_when_page_is_not_allowed() {
$page_slug = 'some_page';
WP_Mock::userFunction( 'get_current_screen' )->andReturn( null );
$subject = new Admin_Notices();
$subject->add_notice( 'Message', 'notice', [ 'screen_ids' => $page_slug ] );
ob_start();
$subject->show_notices();
$result = ob_get_clean();
self::assertEmpty( $result );
}
/**
* Test add_notice() and show_notices() when page is allowed
*/
public function test_show_notices_when_page_is_allowed() {
$expected = ' <div class="notice">
<p>
<strong>
Message </strong>
</p>
</div>
';
$page_slug = 'some_page';
$current_screen = (object) [ 'id' => $page_slug ];
WP_Mock::userFunction( 'get_current_screen' )->andReturn( $current_screen );
$subject = new Admin_Notices();
WP_Mock::passthruFunction( 'wp_kses_post' );
$subject->add_notice( 'Message', 'notice', [ 'screen_ids' => $page_slug ] );
ob_start();
$subject->show_notices();
$result = ob_get_clean();
self::assertSame( $expected, $result );
}
}