@@ -236,6 +236,275 @@ public function should_handle_scalar_values_in_queries() {
236236 $ this ->assertEquals ( 'Scalar Test ' , $ result ['name ' ] );
237237 }
238238
239+ /**
240+ * @test
241+ */
242+ public function should_paginate_with_simple_where_clause () {
243+ $ table = $ this ->get_query_test_table ();
244+ Register::table ( $ table );
245+
246+ // Insert test data
247+ $ table ::insert ( [ 'name ' => 'Item 1 ' , 'slug ' => 'item-1 ' , 'status ' => 1 ] );
248+ $ table ::insert ( [ 'name ' => 'Item 2 ' , 'slug ' => 'item-2 ' , 'status ' => 1 ] );
249+ $ table ::insert ( [ 'name ' => 'Item 3 ' , 'slug ' => 'item-3 ' , 'status ' => 0 ] );
250+ $ table ::insert ( [ 'name ' => 'Item 4 ' , 'slug ' => 'item-4 ' , 'status ' => 1 ] );
251+
252+ // Query with simple where clause
253+ $ results = $ table ::paginate (
254+ [
255+ [
256+ 'column ' => 'status ' ,
257+ 'value ' => 1 ,
258+ 'operator ' => '= ' ,
259+ ],
260+ ],
261+ 10 ,
262+ 1
263+ );
264+
265+ $ this ->assertCount ( 3 , $ results );
266+ }
267+
268+ /**
269+ * @test
270+ */
271+ public function should_paginate_with_sub_where_clauses_using_or () {
272+ $ table = $ this ->get_query_test_table ();
273+ Register::table ( $ table );
274+
275+ // Insert test data
276+ $ table ::insert ( [ 'name ' => 'Alpha ' , 'slug ' => 'alpha ' , 'status ' => 1 ] );
277+ $ table ::insert ( [ 'name ' => 'Beta ' , 'slug ' => 'beta ' , 'status ' => 0 ] );
278+ $ table ::insert ( [ 'name ' => 'Gamma ' , 'slug ' => 'gamma ' , 'status ' => 1 ] );
279+ $ table ::insert ( [ 'name ' => 'Delta ' , 'slug ' => 'delta ' , 'status ' => 0 ] );
280+
281+ // Query: WHERE (slug = 'alpha' OR slug = 'beta')
282+ $ results = $ table ::paginate (
283+ [
284+ [
285+ 'query_operator ' => 'OR ' ,
286+ [
287+ 'column ' => 'slug ' ,
288+ 'value ' => 'alpha ' ,
289+ 'operator ' => '= ' ,
290+ ],
291+ [
292+ 'column ' => 'slug ' ,
293+ 'value ' => 'beta ' ,
294+ 'operator ' => '= ' ,
295+ ],
296+ ],
297+ ],
298+ 10 ,
299+ 1
300+ );
301+
302+ $ this ->assertCount ( 2 , $ results );
303+ $ slugs = array_column ( $ results , 'slug ' );
304+ $ this ->assertContains ( 'alpha ' , $ slugs );
305+ $ this ->assertContains ( 'beta ' , $ slugs );
306+ }
307+
308+ /**
309+ * @test
310+ */
311+ public function should_paginate_with_nested_sub_where_clauses () {
312+ $ table = $ this ->get_query_test_table ();
313+ Register::table ( $ table );
314+
315+ // Insert test data
316+ $ table ::insert ( [ 'name ' => 'Active Alpha ' , 'slug ' => 'alpha ' , 'status ' => 1 ] );
317+ $ table ::insert ( [ 'name ' => 'Inactive Alpha ' , 'slug ' => 'alpha-inactive ' , 'status ' => 0 ] );
318+ $ table ::insert ( [ 'name ' => 'Active Beta ' , 'slug ' => 'beta ' , 'status ' => 1 ] );
319+ $ table ::insert ( [ 'name ' => 'Inactive Beta ' , 'slug ' => 'beta-inactive ' , 'status ' => 0 ] );
320+ $ table ::insert ( [ 'name ' => 'Active Gamma ' , 'slug ' => 'gamma ' , 'status ' => 1 ] );
321+
322+ // Query: WHERE (slug = 'alpha' OR slug = 'beta') AND status = 1
323+ $ results = $ table ::paginate (
324+ [
325+ [
326+ 'query_operator ' => 'OR ' ,
327+ [
328+ 'column ' => 'slug ' ,
329+ 'value ' => 'alpha ' ,
330+ 'operator ' => '= ' ,
331+ ],
332+ [
333+ 'column ' => 'slug ' ,
334+ 'value ' => 'beta ' ,
335+ 'operator ' => '= ' ,
336+ ],
337+ ],
338+ [
339+ 'column ' => 'status ' ,
340+ 'value ' => 1 ,
341+ 'operator ' => '= ' ,
342+ ],
343+ ],
344+ 10 ,
345+ 1
346+ );
347+
348+ $ this ->assertCount ( 2 , $ results );
349+ $ slugs = array_column ( $ results , 'slug ' );
350+ $ this ->assertContains ( 'alpha ' , $ slugs );
351+ $ this ->assertContains ( 'beta ' , $ slugs );
352+
353+ // Verify all results have status = 1
354+ foreach ( $ results as $ result ) {
355+ $ this ->assertEquals ( 1 , $ result ['status ' ] );
356+ }
357+ }
358+
359+ /**
360+ * @test
361+ */
362+ public function should_paginate_with_multiple_sub_where_groups () {
363+ $ table = $ this ->get_query_test_table ();
364+ Register::table ( $ table );
365+
366+ // Insert test data
367+ $ table ::insert ( [ 'name ' => 'A ' , 'slug ' => 'a ' , 'status ' => 1 ] );
368+ $ table ::insert ( [ 'name ' => 'B ' , 'slug ' => 'b ' , 'status ' => 0 ] );
369+ $ table ::insert ( [ 'name ' => 'C ' , 'slug ' => 'c ' , 'status ' => 1 ] );
370+ $ table ::insert ( [ 'name ' => 'D ' , 'slug ' => 'd ' , 'status ' => 0 ] );
371+
372+ // Query with OR at the top level: WHERE (slug = 'a') OR (slug = 'b')
373+ $ results = $ table ::paginate (
374+ [
375+ 'query_operator ' => 'OR ' ,
376+ [
377+ 'column ' => 'slug ' ,
378+ 'value ' => 'a ' ,
379+ 'operator ' => '= ' ,
380+ ],
381+ [
382+ 'column ' => 'slug ' ,
383+ 'value ' => 'b ' ,
384+ 'operator ' => '= ' ,
385+ ],
386+ ],
387+ 10 ,
388+ 1
389+ );
390+
391+ $ this ->assertCount ( 2 , $ results );
392+ }
393+
394+ /**
395+ * @test
396+ */
397+ public function should_count_total_items_with_sub_where_clauses () {
398+ $ table = $ this ->get_query_test_table ();
399+ Register::table ( $ table );
400+
401+ // Insert test data
402+ $ table ::insert ( [ 'name ' => 'X ' , 'slug ' => 'x ' , 'status ' => 1 ] );
403+ $ table ::insert ( [ 'name ' => 'Y ' , 'slug ' => 'y ' , 'status ' => 0 ] );
404+ $ table ::insert ( [ 'name ' => 'Z ' , 'slug ' => 'z ' , 'status ' => 1 ] );
405+
406+ // Count with sub-where: WHERE (slug = 'x' OR slug = 'y')
407+ $ total = $ table ::get_total_items (
408+ [
409+ [
410+ 'query_operator ' => 'OR ' ,
411+ [
412+ 'column ' => 'slug ' ,
413+ 'value ' => 'x ' ,
414+ 'operator ' => '= ' ,
415+ ],
416+ [
417+ 'column ' => 'slug ' ,
418+ 'value ' => 'y ' ,
419+ 'operator ' => '= ' ,
420+ ],
421+ ],
422+ ]
423+ );
424+
425+ $ this ->assertEquals ( 2 , $ total );
426+ }
427+
428+ /**
429+ * @test
430+ */
431+ public function should_handle_sub_where_with_different_operators () {
432+ $ table = $ this ->get_query_test_table ();
433+ Register::table ( $ table );
434+
435+ // Insert test data
436+ $ table ::insert ( [ 'name ' => 'First ' , 'slug ' => 'first ' , 'status ' => 1 ] );
437+ $ table ::insert ( [ 'name ' => 'Second ' , 'slug ' => 'second ' , 'status ' => 2 ] );
438+ $ table ::insert ( [ 'name ' => 'Third ' , 'slug ' => 'third ' , 'status ' => 3 ] );
439+ $ table ::insert ( [ 'name ' => 'Fourth ' , 'slug ' => 'fourth ' , 'status ' => 4 ] );
440+
441+ // Query: WHERE (status > 1 AND status < 4)
442+ $ results = $ table ::paginate (
443+ [
444+ [
445+ 'query_operator ' => 'AND ' ,
446+ [
447+ 'column ' => 'status ' ,
448+ 'value ' => 1 ,
449+ 'operator ' => '> ' ,
450+ ],
451+ [
452+ 'column ' => 'status ' ,
453+ 'value ' => 4 ,
454+ 'operator ' => '< ' ,
455+ ],
456+ ],
457+ ],
458+ 10 ,
459+ 1
460+ );
461+
462+ $ this ->assertCount ( 2 , $ results );
463+ $ statuses = array_column ( $ results , 'status ' );
464+ $ this ->assertContains ( 2 , $ statuses );
465+ $ this ->assertContains ( 3 , $ statuses );
466+ }
467+
468+ /**
469+ * @test
470+ */
471+ public function should_use_stellarwp_schema_hook_prefix () {
472+ $ table = $ this ->get_query_test_table ();
473+ Register::table ( $ table );
474+
475+ $ table ::insert ( [ 'name ' => 'Hook Test ' , 'slug ' => 'hook-test ' , 'status ' => 1 ] );
476+
477+ $ pre_results_fired = false ;
478+ $ post_results_fired = false ;
479+ $ results_filter_fired = false ;
480+ $ where_filter_fired = false ;
481+
482+ add_action ( 'stellarwp_schema_custom_table_query_pre_results ' , function () use ( &$ pre_results_fired ) {
483+ $ pre_results_fired = true ;
484+ } );
485+
486+ add_action ( 'stellarwp_schema_custom_table_query_post_results ' , function () use ( &$ post_results_fired ) {
487+ $ post_results_fired = true ;
488+ } );
489+
490+ add_filter ( 'stellarwp_schema_custom_table_query_results ' , function ( $ results ) use ( &$ results_filter_fired ) {
491+ $ results_filter_fired = true ;
492+ return $ results ;
493+ } );
494+
495+ add_filter ( 'stellarwp_schema_custom_table_query_where ' , function ( $ where ) use ( &$ where_filter_fired ) {
496+ $ where_filter_fired = true ;
497+ return $ where ;
498+ } );
499+
500+ $ table ::paginate ( [], 10 , 1 );
501+
502+ $ this ->assertTrue ( $ pre_results_fired , 'stellarwp_schema_custom_table_query_pre_results action should fire ' );
503+ $ this ->assertTrue ( $ post_results_fired , 'stellarwp_schema_custom_table_query_post_results action should fire ' );
504+ $ this ->assertTrue ( $ results_filter_fired , 'stellarwp_schema_custom_table_query_results filter should fire ' );
505+ $ this ->assertTrue ( $ where_filter_fired , 'stellarwp_schema_custom_table_query_where filter should fire ' );
506+ }
507+
239508 /**
240509 * Get a test table for query method testing.
241510 */
0 commit comments