1+ <?php 
2+ 
3+ use  PHPUnit \Framework \TestCase ;
4+ 
5+ /** 
6+  * Tests for Hm_Saved_Searches class 
7+  * Comprehensive coverage of both search types (simple and advanced): 
8+  */ 
9+ class  Hm_Test_Saved_Searches_Class extends  TestCase {
10+ 
11+     private  $ saved_searches
12+     private  $ sample_data
13+     private  $ advanced_search_data
14+ 
15+     public  function  setUp (): void  {
16+         require_once  __DIR__ .'/../../bootstrap.php ' ;
17+         require_once  APP_PATH .'modules/core/modules.php ' ;
18+         require_once  APP_PATH .'modules/saved_searches/modules.php ' ;
19+ 
20+         $ this sample_data  = array (
21+             'Simple Search '  => array (
22+                 'test terms ' ,
23+                 'SINCE 1-Jan-2023 ' , 
24+                 'TEXT ' ,
25+                 'Simple Search ' 
26+             ),
27+             'Another Simple '  => array (
28+                 'more terms ' ,
29+                 'SINCE 1-Jun-2023 ' ,
30+                 'SUBJECT ' , 
31+                 'Another Simple ' 
32+             )
33+         );
34+ 
35+         $ this advanced_search_data  = array (
36+             'terms '  => array (
37+                 array ('term '  => 'project meeting ' , 'condition '  => 'and ' ),
38+                 array ('term '  => 'urgent deadline ' , 'condition '  => 'or ' ),
39+                 array ('term '  => 'quarterly report ' , 'condition '  => 'and ' )
40+             ),
41+             'targets '  => array (
42+                 array ('target '  => 'SUBJECT ' , 'orig '  => 'TEXT ' , 'condition '  => 'and ' ),
43+                 array ('target '  => 'FROM ' , 'orig '  => 'SUBJECT ' , 'condition '  => 'or ' ),
44+                 array ('target '  => 'TO ' , 'orig '  => 'FROM ' , 'condition '  => 'and ' )
45+             ),
46+             'sources '  => array (
47+                 array ('source '  => 'imap_0_INBOX ' , 'label '  => 'Gmail - Inbox ' ),
48+                 array ('source '  => 'imap_1_INBOX.Sent ' , 'label '  => 'Yahoo - Sent Mail ' ),
49+                 array ('source '  => 'imap_2_Work.Projects ' , 'label '  => 'Work Email - Projects ' )
50+             ),
51+             'times '  => array (
52+                 array ('from '  => '2024-01-01 ' , 'to '  => '2024-12-31 ' )
53+             ),
54+             'other '  => array (
55+                 'charset '  => 'UTF-8 ' ,
56+                 'limit '  => 100 ,
57+                 'flags '  => array ('SEEN ' )
58+             )
59+         );
60+ 
61+         $ this saved_searches  = new  Hm_Saved_Searches ($ this sample_data );
62+     }
63+ 
64+     /** 
65+      * @preserveGlobalState disabled 
66+      * @runInSeparateProcess 
67+      */ 
68+     public  function  test_constructor_and_dump () {
69+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
70+         $ result$ searchesdump ();
71+ 
72+         $ keysarray_keys ($ result
73+         $ this assertEquals ('Another Simple ' , $ keys0 ]);
74+         $ this assertEquals ('Simple Search ' , $ keys1 ]);
75+     }
76+ 
77+     /** 
78+      * @preserveGlobalState disabled 
79+      * @runInSeparateProcess 
80+      */ 
81+     public  function  test_add_simple_search () {
82+         $ searchesnew  Hm_Saved_Searches (array ());
83+         $ search_dataarray ('new terms ' , 'SINCE 1-Jan-2024 ' , 'FROM ' , 'New Search ' );
84+ 
85+         $ result$ searchesadd ('New Search ' , $ search_data
86+ 
87+         $ this assertTrue ($ result
88+         $ this assertEquals ($ search_data$ searchesget ('New Search ' ));
89+     }
90+ 
91+     /** 
92+      * @preserveGlobalState disabled 
93+      * @runInSeparateProcess 
94+      */ 
95+     public  function  test_add_duplicate_search () {
96+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
97+         $ search_dataarray ('duplicate ' , 'SINCE 1-Jan-2024 ' , 'TEXT ' , 'Simple Search ' );
98+ 
99+         $ result$ searchesadd ('Simple Search ' , $ search_data
100+ 
101+         $ this assertFalse ($ result
102+     }
103+ 
104+     /** 
105+      * @preserveGlobalState disabled 
106+      * @runInSeparateProcess 
107+      */ 
108+     public  function  test_update_existing_search () {
109+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
110+         $ new_dataarray ('updated terms ' , 'SINCE 1-Jan-2024 ' , 'FROM ' , 'Simple Search ' );
111+ 
112+         $ result$ searchesupdate ('Simple Search ' , $ new_data
113+ 
114+         $ this assertTrue ($ result
115+         $ this assertEquals ($ new_data$ searchesget ('Simple Search ' ));
116+     }
117+ 
118+     /** 
119+      * @preserveGlobalState disabled 
120+      * @runInSeparateProcess 
121+      */ 
122+     public  function  test_update_nonexistent_search () {
123+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
124+         $ new_dataarray ('updated terms ' , 'SINCE 1-Jan-2024 ' , 'FROM ' , 'Nonexistent ' );
125+ 
126+         $ result$ searchesupdate ('Nonexistent ' , $ new_data
127+ 
128+         $ this assertFalse ($ result
129+     }
130+ 
131+     /** 
132+      * @preserveGlobalState disabled 
133+      * @runInSeparateProcess 
134+      */ 
135+     public  function  test_delete_existing_search () {
136+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
137+ 
138+         $ result$ searchesdelete ('Simple Search ' );
139+ 
140+         $ this assertTrue ($ result
141+         $ this assertFalse ($ searchesget ('Simple Search ' ));
142+         $ this assertEquals (1 , count ($ searchesdump ()));
143+     }
144+ 
145+     /** 
146+      * @preserveGlobalState disabled 
147+      * @runInSeparateProcess 
148+      */ 
149+     public  function  test_delete_nonexistent_search () {
150+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
151+ 
152+         $ result$ searchesdelete ('Nonexistent ' );
153+ 
154+         $ this assertFalse ($ result
155+         $ this assertEquals (2 , count ($ searchesdump ()));
156+     }
157+ 
158+     /** 
159+      * @preserveGlobalState disabled 
160+      * @runInSeparateProcess 
161+      */ 
162+     public  function  test_rename_search () {
163+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
164+ 
165+         $ result$ searchesrename ('Simple Search ' , 'Renamed Search ' );
166+ 
167+         $ this assertTrue ($ result
168+         $ this assertFalse ($ searchesget ('Simple Search ' ));
169+         $ this assertNotFalse ($ searchesget ('Renamed Search ' ));
170+     }
171+ 
172+     /** 
173+      * @preserveGlobalState disabled 
174+      * @runInSeparateProcess 
175+      */ 
176+     public  function  test_add_advanced_search () {
177+         $ searchesnew  Hm_Saved_Searches (array ());
178+ 
179+         $ result$ searchesadd_advanced ('My Advanced Search ' , $ this advanced_search_data );
180+ 
181+         $ this assertTrue ($ result
182+         $ stored$ searchesget ('My Advanced Search ' );
183+         $ this assertEquals ('advanced ' , $ stored'type ' ]);
184+         $ this assertEquals ($ this advanced_search_data , $ stored'data ' ]);
185+         $ this assertEquals ('My Advanced Search ' , $ stored'name ' ]);
186+     }
187+ 
188+     /** 
189+      * @preserveGlobalState disabled 
190+      * @runInSeparateProcess 
191+      */ 
192+     public  function  test_update_advanced_search () {
193+         $ searchesnew  Hm_Saved_Searches (array ());
194+         $ searchesadd_advanced ('Advanced Search ' , $ this advanced_search_data );
195+ 
196+         $ updated_data$ this advanced_search_data ;
197+         $ updated_data'terms ' ][0 ]['term ' ] = 'updated ' ;
198+ 
199+         $ result$ searchesupdate_advanced ('Advanced Search ' , $ updated_data
200+ 
201+         $ this assertTrue ($ result
202+         $ stored$ searchesget_advanced ('Advanced Search ' );
203+         $ this assertEquals ('updated ' , $ stored'terms ' ][0 ]['term ' ]);
204+     }
205+ 
206+     /** 
207+      * @preserveGlobalState disabled 
208+      * @runInSeparateProcess 
209+      */ 
210+     public  function  test_update_advanced_search_that_is_not_advanced () {
211+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
212+ 
213+         $ result$ searchesupdate_advanced ('Simple Search ' , $ this advanced_search_data );
214+ 
215+         $ this assertFalse ($ result
216+     }
217+ 
218+     /** 
219+      * @preserveGlobalState disabled 
220+      * @runInSeparateProcess 
221+      */ 
222+     public  function  test_is_advanced () {
223+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
224+         $ searchesadd_advanced ('Advanced Search ' , $ this advanced_search_data );
225+ 
226+         $ this assertFalse ($ searchesis_advanced ('Simple Search ' ));
227+         $ this assertTrue ($ searchesis_advanced ('Advanced Search ' ));
228+         $ this assertFalse ($ searchesis_advanced ('Nonexistent ' ));
229+     }
230+ 
231+     /** 
232+      * @preserveGlobalState disabled 
233+      * @runInSeparateProcess 
234+      */ 
235+     public  function  test_get_advanced () {
236+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
237+         $ searchesadd_advanced ('Advanced Search ' , $ this advanced_search_data );
238+ 
239+         $ result$ searchesget_advanced ('Advanced Search ' );
240+         $ this assertEquals ($ this advanced_search_data , $ result
241+ 
242+         $ result$ searchesget_advanced ('Simple Search ' );
243+         $ this assertFalse ($ result
244+ 
245+         $ result$ searchesget_advanced ('Nonexistent ' , 'default ' );
246+         $ this assertEquals ('default ' , $ result
247+     }
248+ 
249+     /** 
250+      * @preserveGlobalState disabled 
251+      * @runInSeparateProcess 
252+      */ 
253+     public  function  test_get_by_type () {
254+         $ searchesnew  Hm_Saved_Searches ($ this sample_data );
255+         $ searchesadd_advanced ('Advanced Search 1 ' , $ this advanced_search_data );
256+         $ searchesadd_advanced ('Advanced Search 2 ' , $ this advanced_search_data );
257+ 
258+         $ result$ searchesget_by_type ();
259+ 
260+         $ this assertArrayHasKey ('simple ' , $ result
261+         $ this assertArrayHasKey ('advanced ' , $ result
262+         $ this assertEquals (2 , count ($ result'simple ' ]));
263+         $ this assertEquals (2 , count ($ result'advanced ' ]));
264+ 
265+         $ this assertArrayHasKey ('Simple Search ' , $ result'simple ' ]);
266+         $ this assertArrayHasKey ('Another Simple ' , $ result'simple ' ]);
267+ 
268+         $ this assertArrayHasKey ('Advanced Search 1 ' , $ result'advanced ' ]);
269+         $ this assertArrayHasKey ('Advanced Search 2 ' , $ result'advanced ' ]);
270+     }
271+ 
272+     /** 
273+      * @preserveGlobalState disabled 
274+      * @runInSeparateProcess 
275+      */ 
276+     public  function  test_get_by_type_empty () {
277+         $ searchesnew  Hm_Saved_Searches (array ());
278+ 
279+         $ result$ searchesget_by_type ();
280+ 
281+         $ this assertEquals (array ('simple '  => array (), 'advanced '  => array ()), $ result
282+     }
283+ }
0 commit comments