| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Libs\Rdbms; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use Wikimedia\Rdbms\AndExpressionGroup; |
| 8 |
use Wikimedia\Rdbms\Expression; |
| 9 |
use Wikimedia\Rdbms\IExpression; |
| 10 |
use Wikimedia\Rdbms\LikeMatch; |
| 11 |
use Wikimedia\Rdbms\LikeValue; |
| 12 |
use Wikimedia\Rdbms\OrExpressionGroup; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class ExpressionTest extends TestCase { |
| 23 |
|
| 24 |
public static function provideSimple() { |
| 25 |
return [ |
| 26 |
[ |
| 27 |
'rev_timestamp', '>', '20221025145309', |
| 28 |
"rev_timestamp > '20221025145309'" |
| 29 |
], |
| 30 |
[ |
| 31 |
'rev_timestamp', '>=', '20221025145309', |
| 32 |
"rev_timestamp >= '20221025145309'" |
| 33 |
], |
| 34 |
[ |
| 35 |
'rev_timestamp', '<', '20221025145309', |
| 36 |
"rev_timestamp < '20221025145309'" |
| 37 |
], |
| 38 |
[ |
| 39 |
'rev_timestamp', '<=', '20221025145309', |
| 40 |
"rev_timestamp <= '20221025145309'" |
| 41 |
], |
| 42 |
[ |
| 43 |
'rev_timestamp', '=', '20221025145309', |
| 44 |
"rev_timestamp = '20221025145309'" |
| 45 |
], |
| 46 |
[ |
| 47 |
'rev_timestamp', '!=', '20221025145309', |
| 48 |
"rev_timestamp != '20221025145309'" |
| 49 |
], |
| 50 |
[ |
| 51 |
'revision.rev_timestamp', '!=', '20221025145309', |
| 52 |
"revision.rev_timestamp != '20221025145309'" |
| 53 |
], |
| 54 |
[ |
| 55 |
'rev_id', '>', 12345, |
| 56 |
"rev_id > 12345" |
| 57 |
], |
| 58 |
[ |
| 59 |
'rev_id', '>=', 12345, |
| 60 |
"rev_id >= 12345" |
| 61 |
], |
| 62 |
[ |
| 63 |
'rev_id', '<', 12345, |
| 64 |
"rev_id < 12345" |
| 65 |
], |
| 66 |
[ |
| 67 |
'rev_id', '<=', 12345, |
| 68 |
"rev_id <= 12345" |
| 69 |
], |
| 70 |
[ |
| 71 |
'rev_id', '=', 12345, |
| 72 |
"rev_id = 12345" |
| 73 |
], |
| 74 |
[ |
| 75 |
'rev_id', '=', [ 'x' ], |
| 76 |
"rev_id = 'x'" |
| 77 |
], |
| 78 |
[ |
| 79 |
'rev_id', '!=', 12345, |
| 80 |
"rev_id != 12345" |
| 81 |
], |
| 82 |
[ |
| 83 |
'rev_id', '!=', [ 'x' ], |
| 84 |
"rev_id != 'x'" |
| 85 |
], |
| 86 |
[ |
| 87 |
'rev_id', '=', [ 12345, 456 ], |
| 88 |
"rev_id IN (12345,456)" |
| 89 |
], |
| 90 |
[ |
| 91 |
'rev_id', '!=', [ 12345, 456 ], |
| 92 |
"rev_id NOT IN (12345,456)" |
| 93 |
], |
| 94 |
[ |
| 95 |
'rc_old_len', '=', null, |
| 96 |
"rc_old_len IS NULL" |
| 97 |
], |
| 98 |
[ |
| 99 |
'rc_old_len', '!=', null, |
| 100 |
"rc_old_len IS NOT NULL" |
| 101 |
], |
| 102 |
[ |
| 103 |
'rev_timestamp', '=', [ '20221025145309', '20230919152602' ], |
| 104 |
"rev_timestamp IN ('20221025145309','20230919152602')" |
| 105 |
], |
| 106 |
[ |
| 107 |
'rev_timestamp', '!=', [ '20221025145309', '20230919152602' ], |
| 108 |
"rev_timestamp NOT IN ('20221025145309','20230919152602')" |
| 109 |
], |
| 110 |
[ |
| 111 |
'user_name', IExpression::LIKE, new LikeValue( 'foo', new LikeMatch( '%' ) ), |
| 112 |
"user_name LIKE 'foo%' ESCAPE '`'" |
| 113 |
], |
| 114 |
[ |
| 115 |
'user_name', IExpression::NOT_LIKE, new LikeValue( new LikeMatch( '%' ), 'foo' ), |
| 116 |
"user_name NOT LIKE '%foo' ESCAPE '`'" |
| 117 |
], |
| 118 |
[ |
| 119 |
'user_name', IExpression::LIKE, new LikeValue( 'foo' ), |
| 120 |
"user_name LIKE 'foo' ESCAPE '`'" |
| 121 |
], |
| 122 |
[ |
| 123 |
'user_name', IExpression::LIKE, new LikeValue( new LikeMatch( '_' ), '%_', new LikeMatch( '%' ) ), |
| 124 |
"user_name LIKE '_`%`_%' ESCAPE '`'" |
| 125 |
], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function testSimple( $field, $op, $value, $sql ) { |
| 133 |
$expr = new Expression( $field, $op, $value ); |
| 134 |
$this->assertSame( $sql, $expr->toSql( new AddQuoterMock() ) ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function provideSimpleInvalid() { |
| 138 |
return [ |
| 139 |
[ 'rev_timestamp', 'bigger', '12345' ], |
| 140 |
[ 'rev_timestamp', 'bigger', 1234 ], |
| 141 |
[ 'rev_timestamp', '=', [] ], |
| 142 |
[ 'rev_timestamp', '=', [ 1234, null, 456 ] ], |
| 143 |
[ 'rev_timestamp', '>', [ 1234, 456 ] ], |
| 144 |
[ 'rev_timestamp', '<', [ 1234, 456 ] ], |
| 145 |
[ 'rev_timestamp', '>', null ], |
| 146 |
[ 'rev_timestamp', '<', null ], |
| 147 |
[ 'rev_timestamp', '=<', '12345' ], |
| 148 |
'associative array' => [ 'rev_timestamp', '=', [ 'a' => 1234, 'b' => 456 ] ], |
| 149 |
'associative array (single)' => [ 'rev_timestamp', '=', [ 'a' => 1234 ] ], |
| 150 |
'array with gaps' => [ 'rev_timestamp', '=', array_unique( [ 1234, 1234, 456, 456 ] ) ], |
| 151 |
[ 'user_name', IExpression::LIKE, 'foo' ], |
| 152 |
[ 'user_name', IExpression::LIKE, [ 'foo' ] ], |
| 153 |
[ 'user_name', '=', new LikeValue( 'foo' ) ], |
| 154 |
[ 'SELECT user_password from user where user_id = 1', '>', 1234 ], |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
public function testSimpleInvalid( $field, $op, $value ): void { |
| 160 |
$this->expectException( InvalidArgumentException::class ); |
| 161 |
$expr = new Expression( $field, $op, $value ); |
| 162 |
$expr->toSql( new AddQuoterMock() ); |
| 163 |
} |
| 164 |
|
| 165 |
public function testSimpleChain() { |
| 166 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 167 |
->and( 'rev_timestamp', '<', '20230919152602' ); |
| 168 |
$this->assertSame( |
| 169 |
"(rev_timestamp > '20221025145309' AND rev_timestamp < '20230919152602')", |
| 170 |
$expr->toSql( new AddQuoterMock() ) |
| 171 |
); |
| 172 |
|
| 173 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 174 |
->or( 'rev_timestamp', '<', '20230919152602' ); |
| 175 |
$this->assertSame( |
| 176 |
"(rev_timestamp > '20221025145309' OR rev_timestamp < '20230919152602')", |
| 177 |
$expr->toSql( new AddQuoterMock() ) |
| 178 |
); |
| 179 |
|
| 180 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 181 |
->and( 'rev_id', '=', [ 1234, 4567 ] ); |
| 182 |
$this->assertSame( |
| 183 |
"(rev_timestamp > '20221025145309' AND rev_id IN (1234,4567))", |
| 184 |
$expr->toSql( new AddQuoterMock() ) |
| 185 |
); |
| 186 |
|
| 187 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 188 |
->or( 'rev_id', '=', [ 1234, 4567 ] ); |
| 189 |
$this->assertSame( |
| 190 |
"(rev_timestamp > '20221025145309' OR rev_id IN (1234,4567))", |
| 191 |
$expr->toSql( new AddQuoterMock() ) |
| 192 |
); |
| 193 |
|
| 194 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 195 |
->and( 'rc_old_len', '!=', null ); |
| 196 |
$this->assertSame( |
| 197 |
"(rev_timestamp > '20221025145309' AND rc_old_len IS NOT NULL)", |
| 198 |
$expr->toSql( new AddQuoterMock() ) |
| 199 |
); |
| 200 |
|
| 201 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 202 |
->or( 'rc_old_len', '!=', null ); |
| 203 |
$this->assertSame( |
| 204 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NOT NULL)", |
| 205 |
$expr->toSql( new AddQuoterMock() ) |
| 206 |
); |
| 207 |
|
| 208 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 209 |
->and( 'rc_old_len', '=', null ); |
| 210 |
$this->assertSame( |
| 211 |
"(rev_timestamp > '20221025145309' AND rc_old_len IS NULL)", |
| 212 |
$expr->toSql( new AddQuoterMock() ) |
| 213 |
); |
| 214 |
|
| 215 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 216 |
->or( 'rc_old_len', '=', null ); |
| 217 |
$this->assertSame( |
| 218 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL)", |
| 219 |
$expr->toSql( new AddQuoterMock() ) |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
public function testComplexChain() { |
| 224 |
$expr = ( new Expression( 'rev_timestamp', '>', '20221025145309' ) ) |
| 225 |
->or( 'rc_old_len', '=', null ) |
| 226 |
->or( 'rc_new_len', '=', null ); |
| 227 |
$this->assertSame( |
| 228 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL OR rc_new_len IS NULL)", |
| 229 |
$expr->toSql( new AddQuoterMock() ) |
| 230 |
); |
| 231 |
|
| 232 |
$expr2 = ( new Expression( 'rev_id', '=', [ 123, 456 ] ) ) |
| 233 |
->andExpr( $expr ); |
| 234 |
$this->assertSame( |
| 235 |
"(rev_id IN (123,456) AND " . |
| 236 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL OR rc_new_len IS NULL))", |
| 237 |
$expr2->toSql( new AddQuoterMock() ) |
| 238 |
); |
| 239 |
|
| 240 |
$expr3 = ( new Expression( 'rev_actor', '>', 3 ) ) |
| 241 |
->orExpr( $expr2 ); |
| 242 |
$this->assertSame( |
| 243 |
"(rev_actor > 3 OR (rev_id IN (123,456) AND " . |
| 244 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL OR rc_new_len IS NULL)))", |
| 245 |
$expr3->toSql( new AddQuoterMock() ) |
| 246 |
); |
| 247 |
|
| 248 |
$expr4 = ( new AndExpressionGroup( new Expression( 'rev_id', '=', [ 123, 456 ] ) ) ) |
| 249 |
->andExpr( $expr ); |
| 250 |
$this->assertSame( |
| 251 |
"(rev_id IN (123,456) AND " . |
| 252 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL OR rc_new_len IS NULL))", |
| 253 |
$expr4->toSql( new AddQuoterMock() ) |
| 254 |
); |
| 255 |
|
| 256 |
$expr5 = ( new OrExpressionGroup( new Expression( 'rev_id', '=', [ 123, 456 ] ) ) ) |
| 257 |
->orExpr( $expr2 ); |
| 258 |
$this->assertSame( |
| 259 |
"(rev_id IN (123,456) OR (rev_id IN (123,456) AND " . |
| 260 |
"(rev_timestamp > '20221025145309' OR rc_old_len IS NULL OR rc_new_len IS NULL)))", |
| 261 |
$expr5->toSql( new AddQuoterMock() ) |
| 262 |
); |
| 263 |
} |
| 264 |
} |