1515 */
1616class Test_WP_Key_Pair_List_Table extends WP_UnitTestCase {
1717
18+ /**
19+ * @var WP_Key_Pair_List_Table
20+ */
21+ protected $ table ;
22+
23+ function setUp () {
24+ parent ::setUp ();
25+ $ this ->table = new WP_Key_Pair_List_Table ( array ( 'screen ' => 'profile ' ) );
26+ }
27+
1828 /**
1929 * Test get_columns().
2030 *
@@ -30,8 +40,7 @@ public function test_get_columns() {
3040 'token ' ,
3141 'revoke ' ,
3242 );
33- $ table = new WP_Key_Pair_List_Table ( array ( 'screen ' => 'profile ' ) );
34- $ columns = $ table ->get_columns ();
43+ $ columns = $ this ->table ->get_columns ();
3544 foreach ( $ expected as $ column ) {
3645 $ this ->assertArrayHasKey ( $ column , $ columns );
3746 }
@@ -44,7 +53,28 @@ public function test_get_columns() {
4453 * @since 0.1
4554 */
4655 public function test_prepare_items () {
47- $ this ->markTestIncomplete ();
56+ $ this ->table ->items = array (
57+ array (
58+ 'name ' => 'First ' ,
59+ 'api_key ' => '12345 ' ,
60+ ),
61+ array (
62+ 'name ' => 'Second ' ,
63+ 'api_key ' => '54321 ' ,
64+ ),
65+ );
66+ $ this ->table ->prepare_items ();
67+
68+ ob_start ();
69+ $ this ->table ->display ();
70+ $ output = ob_get_clean ();
71+
72+ preg_match_all ( '/<tr data-api_key="(\d+)" data-name="(\S+)"[^>]*>/ ' , $ output , $ matches , PREG_SET_ORDER , 0 );
73+
74+ foreach ( $ this ->table ->items as $ key => $ item ) {
75+ $ this ->assertEquals ( $ item ['api_key ' ], $ matches [ $ key ][1 ] );
76+ $ this ->assertEquals ( $ item ['name ' ], $ matches [ $ key ][2 ] );
77+ }
4878 }
4979
5080 /**
@@ -54,7 +84,93 @@ public function test_prepare_items() {
5484 * @since 0.1
5585 */
5686 public function test_column_default () {
57- $ this ->markTestIncomplete ();
87+ $ tests = array (
88+ array (
89+ 'item ' => array (
90+ 'name ' => 'First ' ,
91+ ),
92+ 'with ' => 'name ' ,
93+ 'want ' => 'First ' ,
94+ ),
95+ array (
96+ 'item ' => array (
97+ 'api_key ' => '12345 ' ,
98+ ),
99+ 'with ' => 'api_key ' ,
100+ 'want ' => '12345 ' ,
101+ ),
102+ array (
103+ 'item ' => array (
104+ 'created ' => '' ,
105+ ),
106+ 'with ' => 'created ' ,
107+ 'want ' => '— ' ,
108+ ),
109+ array (
110+ 'item ' => array (
111+ 'created ' => mktime ( 0 , 0 , 0 , 2 , 1 , 2019 ),
112+ ),
113+ 'with ' => 'created ' ,
114+ 'want ' => 'February 1, 2019 12:00 am ' ,
115+ ),
116+ array (
117+ 'item ' => array (
118+ 'last_used ' => '' ,
119+ ),
120+ 'with ' => 'last_used ' ,
121+ 'want ' => '— ' ,
122+ ),
123+ array (
124+ 'item ' => array (
125+ 'last_used ' => mktime ( 0 , 0 , 0 , 2 , 1 , 2019 ),
126+ ),
127+ 'with ' => 'last_used ' ,
128+ 'want ' => 'February 1, 2019 12:00 am ' ,
129+ ),
130+ array (
131+ 'item ' => array (
132+ 'last_ip ' => '' ,
133+ ),
134+ 'with ' => 'last_ip ' ,
135+ 'want ' => '— ' ,
136+ ),
137+ array (
138+ 'item ' => array (
139+ 'last_ip ' => '127.0.0.1 ' ,
140+ ),
141+ 'with ' => 'last_ip ' ,
142+ 'want ' => '127.0.0.1 ' ,
143+ ),
144+ array (
145+ 'item ' => array (
146+ 'api_key ' => '12345 ' ,
147+ ),
148+ 'with ' => 'token ' ,
149+ 'want ' => get_submit_button ( 'New Token ' , 'secondary ' , 'token-key-pair-12345 ' , false ),
150+ ),
151+ array (
152+ 'item ' => array (
153+ 'api_key ' => '12345 ' ,
154+ ),
155+ 'with ' => 'revoke ' ,
156+ 'want ' => get_submit_button ( 'Revoke ' , 'delete ' , 'revoke-key-pair-12345 ' , false ),
157+ ),
158+ array (
159+ 'item ' => array (
160+ 'api_key ' => '12345 ' ,
161+ ),
162+ 'with ' => 'not_real ' ,
163+ 'want ' => '' ,
164+ ),
165+ );
166+
167+ $ reflection = new ReflectionClass ( get_class ( $ this ->table ) );
168+ $ method = $ reflection ->getMethod ( 'column_default ' );
169+ $ method ->setAccessible ( true );
170+
171+ foreach ( $ tests as $ test ) {
172+ $ this ->assertEquals ( $ test ['want ' ], $ method ->invokeArgs ( $ this ->table , array ( $ test ['item ' ], $ test ['with ' ] ) ) );
173+ }
58174 }
59175
60176 /**
@@ -64,7 +180,11 @@ public function test_column_default() {
64180 * @since 0.1
65181 */
66182 public function test_display_tablenav () {
67- $ this ->markTestIncomplete ();
183+ ob_start ();
184+ $ this ->table ->display_tablenav ( 'bottom ' );
185+ $ output = ob_get_clean ();
186+
187+ $ this ->assertContains ( 'revoke-all-key-pairs ' , $ output );
68188 }
69189
70190 /**
@@ -74,6 +194,18 @@ public function test_display_tablenav() {
74194 * @since 0.1
75195 */
76196 public function test_single_row () {
77- $ this ->markTestIncomplete ();
197+ $ item = array (
198+ 'name ' => 'First ' ,
199+ 'api_key ' => '12345 ' ,
200+ );
201+
202+ ob_start ();
203+ $ this ->table ->single_row ( $ item );
204+ $ output = ob_get_clean ();
205+
206+ preg_match_all ( '/<tr data-api_key="(\d+)" data-name="(\S+)"[^>]*>/ ' , $ output , $ matches , PREG_SET_ORDER , 0 );
207+
208+ $ this ->assertEquals ( $ item ['api_key ' ], $ matches [0 ][1 ] );
209+ $ this ->assertEquals ( $ item ['name ' ], $ matches [0 ][2 ] );
78210 }
79211}
0 commit comments