44use PHPUnit \Framework \TestCase ;
55use PHPUnit \Framework \Error \Notice ;
66
7+ require_once (CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-updater.php ' );
8+
79class TestApbctState extends TestCase
810{
911
@@ -22,27 +24,46 @@ public function testIsHaveErrors_haveErrors()
2224 update_option ( 'cleantalk_errors ' , array ( 'error_type ' => 'Error text ' ) );
2325 $ apbct = new State ( 'cleantalk ' , array ('settings ' , 'data ' , 'errors ' , 'remote_calls ' , 'stats ' , 'fw_stats ' ) );
2426 $ this ->assertTrue ( $ apbct ->isHaveErrors () );
27+ delete_option ('cleantalk_errors ' );
2528 }
2629
2730 public function testIsHaveErrors_emptyErrors ()
2831 {
2932 update_option ( 'cleantalk_errors ' , array () );
3033 $ apbct = new State ( 'cleantalk ' , array ('settings ' , 'data ' , 'errors ' , 'remote_calls ' , 'stats ' , 'fw_stats ' ) );
3134 $ this ->assertFalse ( $ apbct ->isHaveErrors () );
35+ delete_option ('cleantalk_errors ' );
3236 }
3337
3438 public function testIsHaveErrors_emptyInnerErrors ()
3539 {
3640 update_option ( 'cleantalk_errors ' , array ( 'error_type ' => array () ) );
3741 $ apbct = new State ( 'cleantalk ' , array ('settings ' , 'data ' , 'errors ' , 'remote_calls ' , 'stats ' , 'fw_stats ' ) );
3842 $ this ->assertFalse ( $ apbct ->isHaveErrors () );
43+ delete_option ('cleantalk_errors ' );
3944 }
4045
4146 public function testIsHaveErrors_filledInnerErrors ()
4247 {
4348 update_option ( 'cleantalk_errors ' , array ( 'error_type ' => array ( 'error_text ' => 'Error text ' ) ) );
4449 $ apbct = new State ( 'cleantalk ' , array ('settings ' , 'data ' , 'errors ' , 'remote_calls ' , 'stats ' , 'fw_stats ' ) );
4550 $ this ->assertTrue ( $ apbct ->isHaveErrors () );
51+ delete_option ('cleantalk_errors ' );
52+ }
53+
54+ public function testErrorsArrayFromState ()
55+ {
56+ $ apbct = new State ('cleantalk ' , array ('settings ' , 'errors ' ));
57+
58+ $ apbct ->errorAdd ('api ' , 'error ' );
59+
60+ $ errors_from_state = (array ) $ apbct ->errors ;
61+
62+ $ this ->assertArrayHasKey ('api ' , $ errors_from_state );
63+ $ this ->assertArrayHasKey ('error ' , $ errors_from_state ['api ' ][0 ]);
64+ $ this ->assertArrayHasKey ('error_time ' , $ errors_from_state ['api ' ][0 ]);
65+
66+ delete_option ('cleantalk_errors ' );
4667 }
4768
4869 //UpdateVars section
@@ -58,6 +79,7 @@ public function testAutoSaveVars__remote_calls(){
5879 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
5980 $ db_result = get_option ('cleantalk_remote_calls ' )['post_api_key ' ];
6081 $ this ->assertEquals (array ('last_call ' => 0 ,), $ db_result );
82+ delete_option ('cleantalk_remote_calls ' );
6183 }
6284
6385 public function testAutoSaveVars__settings (){
@@ -71,6 +93,7 @@ public function testAutoSaveVars__settings(){
7193 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
7294 $ db_result = get_option ('cleantalk_settings ' )['forms__registrations_test ' ];
7395 $ this ->assertEquals (1 , $ db_result );
96+ delete_option ('cleantalk_settings ' );
7497 }
7598
7699 public function testAutoSaveVars__data (){
@@ -84,6 +107,7 @@ public function testAutoSaveVars__data(){
84107 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
85108 $ db_result = get_option ('cleantalk_data ' )['js_key_lifetime ' ];
86109 $ this ->assertEquals (86400 , $ db_result );
110+ delete_option ('cleantalk_data ' );
87111 }
88112
89113 public function testAutoSaveVars__network_settings (){
@@ -97,6 +121,7 @@ public function testAutoSaveVars__network_settings(){
97121 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
98122 $ db_result = get_option ('cleantalk_network_settings ' )['multisite__white_label__plugin_name ' ];
99123 $ this ->assertEquals ('Anti-Spam by CleanTalk ' , $ db_result );
124+ delete_option ('network_settings ' );
100125 }
101126
102127 public function testAutoSaveVars__network_data (){
@@ -110,6 +135,7 @@ public function testAutoSaveVars__network_data(){
110135 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
111136 $ db_result = get_option ('cleantalk_network_data ' )['moderate ' ];
112137 $ this ->assertEquals (0 , $ db_result );
138+ delete_option ('cleantalk_network_data ' );
113139 }
114140
115141 public function testAutoSaveVars__stats (){
@@ -161,6 +187,7 @@ public function testAutoSaveVars__stats(){
161187 $ this ->assertEquals (14400 , $ db_result );
162188 $ db_result = get_option ('cleantalk_stats ' )['sfw ' ]['sending_logs__timestamp ' ];
163189 $ this ->assertEquals (10000 , $ db_result );
190+ delete_option ('cleantalk_stats ' );
164191
165192 }
166193
@@ -175,6 +202,7 @@ public function testAutoSaveVars__fw_stats(){
175202 apbct_run_update_actions ('6.1 ' ,'6.2 ' );
176203 $ db_result = get_option ('cleantalk_fw_stats ' )['firewall_updating_id ' ];
177204 $ this ->assertEquals (null , $ db_result );
205+ delete_option ('cleantalk_fw_stats ' );
178206 }
179207
180208 public function testAutoSaveVars__fw_stats_await_exception_without_var_updater (){
@@ -191,5 +219,33 @@ public function testAutoSaveVars__fw_stats_await_exception_without_var_updater()
191219 //await udefined index
192220 $ this ->expectException (Notice::class);
193221 $ db_result = get_option ('cleantalk_fw_stats ' )['firewall_updating_id ' ];
222+ delete_option ('cleantalk_fw_stats ' );
223+ }
224+
225+ public function testInit ()
226+ {
227+ $ apbct = new class ('cleantalk ' , array ('settings ' , 'errors ' )) extends State {
228+ protected function isMainSite ()
229+ {
230+ return false ;
231+ }
232+ protected function getWpmsMode ()
233+ {
234+ return 2 ;
235+ }
236+ protected function switchToMainBlog (){}
237+ protected function switchToCurrentBlog (){}
238+
239+ };
240+
241+ $ apbct ->errorAdd ('api ' , 'error ' );
242+
243+ $ errors_from_state = (array ) $ apbct ->errors ;
244+
245+ $ this ->assertArrayHasKey ('api ' , $ errors_from_state );
246+ $ this ->assertArrayHasKey ('error ' , $ errors_from_state ['api ' ][0 ]);
247+ $ this ->assertArrayHasKey ('error_time ' , $ errors_from_state ['api ' ][0 ]);
248+
249+ delete_option ('cleantalk_errors ' );
194250 }
195251}
0 commit comments