|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the Disable_Pagination Trait |
| 4 | + */ |
| 5 | + |
| 6 | +namespace AdvancedQueryLoop\UnitTests; |
| 7 | + |
| 8 | +use AdvancedQueryLoop\Query_Params_Generator; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test the Disable_Pagination trait |
| 13 | + */ |
| 14 | +class Disable_Pagination_Tests extends TestCase { |
| 15 | + |
| 16 | + /** |
| 17 | + * Data provider for tests that should not add no_found_rows |
| 18 | + * |
| 19 | + * @return array |
| 20 | + */ |
| 21 | + public function data_no_pagination_setting() { |
| 22 | + return array( |
| 23 | + 'no disable_pagination param' => array( |
| 24 | + // Custom data. |
| 25 | + array(), |
| 26 | + // Expected. |
| 27 | + array( 'is_aql' => true ), |
| 28 | + ), |
| 29 | + 'empty string disable_pagination' => array( |
| 30 | + // Custom data. |
| 31 | + array( |
| 32 | + 'disable_pagination' => '', |
| 33 | + ), |
| 34 | + // Expected - empty string is falsy, not processed. |
| 35 | + array( 'is_aql' => true ), |
| 36 | + ), |
| 37 | + 'null disable_pagination' => array( |
| 38 | + // Custom data. |
| 39 | + array( |
| 40 | + 'disable_pagination' => null, |
| 41 | + ), |
| 42 | + // Expected - null is falsy, not processed. |
| 43 | + array( 'is_aql' => true ), |
| 44 | + ), |
| 45 | + 'false disable_pagination' => array( |
| 46 | + // Custom data. |
| 47 | + array( |
| 48 | + 'disable_pagination' => false, |
| 49 | + ), |
| 50 | + // Expected - false is falsy, not processed. |
| 51 | + array( 'is_aql' => true ), |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that empty/null/false pagination values are not added |
| 58 | + * |
| 59 | + * @param array $custom_data The params coming from AQL. |
| 60 | + * @param array $expected The expected results. |
| 61 | + * |
| 62 | + * @dataProvider data_no_pagination_setting |
| 63 | + */ |
| 64 | + public function test_disable_pagination_empty_handling( $custom_data, $expected ) { |
| 65 | + $qpg = new Query_Params_Generator( array(), $custom_data ); |
| 66 | + $qpg->process_all(); |
| 67 | + |
| 68 | + $this->assertEquals( $expected, $qpg->get_query_args() ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Data provider for truthy disable_pagination values |
| 73 | + * |
| 74 | + * @return array |
| 75 | + */ |
| 76 | + public function data_disable_pagination_enabled() { |
| 77 | + return array( |
| 78 | + 'boolean true' => array( |
| 79 | + // Custom data. |
| 80 | + array( |
| 81 | + 'disable_pagination' => true, |
| 82 | + ), |
| 83 | + // Expected. |
| 84 | + array( |
| 85 | + 'is_aql' => true, |
| 86 | + 'no_found_rows' => true, |
| 87 | + ), |
| 88 | + ), |
| 89 | + 'integer 1' => array( |
| 90 | + // Custom data. |
| 91 | + array( |
| 92 | + 'disable_pagination' => 1, |
| 93 | + ), |
| 94 | + // Expected. |
| 95 | + array( |
| 96 | + 'is_aql' => true, |
| 97 | + 'no_found_rows' => 1, |
| 98 | + ), |
| 99 | + ), |
| 100 | + 'string "true"' => array( |
| 101 | + // Custom data. |
| 102 | + array( |
| 103 | + 'disable_pagination' => 'true', |
| 104 | + ), |
| 105 | + // Expected. |
| 106 | + array( |
| 107 | + 'is_aql' => true, |
| 108 | + 'no_found_rows' => 'true', |
| 109 | + ), |
| 110 | + ), |
| 111 | + 'string "1"' => array( |
| 112 | + // Custom data. |
| 113 | + array( |
| 114 | + 'disable_pagination' => '1', |
| 115 | + ), |
| 116 | + // Expected. |
| 117 | + array( |
| 118 | + 'is_aql' => true, |
| 119 | + 'no_found_rows' => '1', |
| 120 | + ), |
| 121 | + ), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test that truthy disable_pagination values are passed through |
| 127 | + * |
| 128 | + * @param array $custom_data The params coming from AQL. |
| 129 | + * @param array $expected The expected results. |
| 130 | + * |
| 131 | + * @dataProvider data_disable_pagination_enabled |
| 132 | + */ |
| 133 | + public function test_disable_pagination_enabled( $custom_data, $expected ) { |
| 134 | + $qpg = new Query_Params_Generator( array(), $custom_data ); |
| 135 | + $qpg->process_all(); |
| 136 | + |
| 137 | + $this->assertEquals( $expected, $qpg->get_query_args() ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Test that any non-falsy value is passed through (trait doesn't validate) |
| 142 | + */ |
| 143 | + public function test_disable_pagination_passes_through_any_value() { |
| 144 | + $custom_data = array( |
| 145 | + 'disable_pagination' => 'some-random-value', |
| 146 | + ); |
| 147 | + |
| 148 | + $qpg = new Query_Params_Generator( array(), $custom_data ); |
| 149 | + $qpg->process_all(); |
| 150 | + |
| 151 | + $result = $qpg->get_query_args(); |
| 152 | + |
| 153 | + // The trait simply passes through the value |
| 154 | + $this->assertEquals( 'some-random-value', $result['no_found_rows'] ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Test disable_pagination works with other query params |
| 159 | + */ |
| 160 | + public function test_disable_pagination_with_other_params() { |
| 161 | + $custom_data = array( |
| 162 | + 'disable_pagination' => true, |
| 163 | + 'exclude_current' => 10, |
| 164 | + ); |
| 165 | + |
| 166 | + $qpg = new Query_Params_Generator( array(), $custom_data ); |
| 167 | + $qpg->process_all(); |
| 168 | + |
| 169 | + $result = $qpg->get_query_args(); |
| 170 | + |
| 171 | + // Should have both no_found_rows and post__not_in |
| 172 | + $this->assertArrayHasKey( 'no_found_rows', $result ); |
| 173 | + $this->assertArrayHasKey( 'post__not_in', $result ); |
| 174 | + $this->assertTrue( $result['no_found_rows'] ); |
| 175 | + $this->assertEquals( array( 10 ), $result['post__not_in'] ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Test no_found_rows improves performance (documentation test) |
| 180 | + */ |
| 181 | + public function test_no_found_rows_purpose() { |
| 182 | + // This test documents the purpose of no_found_rows |
| 183 | + // In WordPress, no_found_rows=true skips the SQL_CALC_FOUND_ROWS |
| 184 | + // which improves query performance when pagination info isn't needed |
| 185 | + |
| 186 | + $custom_data = array( |
| 187 | + 'disable_pagination' => true, |
| 188 | + ); |
| 189 | + |
| 190 | + $qpg = new Query_Params_Generator( array(), $custom_data ); |
| 191 | + $qpg->process_all(); |
| 192 | + |
| 193 | + $result = $qpg->get_query_args(); |
| 194 | + |
| 195 | + // Verify the parameter is set correctly for WP_Query |
| 196 | + $this->assertArrayHasKey( 'no_found_rows', $result ); |
| 197 | + $this->assertTrue( $result['no_found_rows'] ); |
| 198 | + } |
| 199 | +} |
0 commit comments