|
9 | 9 | use Illuminate\Support\Carbon; |
10 | 10 | use Illuminate\Support\Facades\DB; |
11 | 11 | use Illuminate\Support\Facades\Schema; |
| 12 | +use Illuminate\Testing\Assert as PHPUnit; |
12 | 13 |
|
13 | 14 | class QueryBuilderTest extends DatabaseTestCase |
14 | 15 | { |
@@ -305,66 +306,256 @@ public function testWhereDate() |
305 | 306 | $this->assertSame(1, DB::table('posts')->whereDate('created_at', new Carbon('2018-01-02'))->count()); |
306 | 307 | } |
307 | 308 |
|
| 309 | + public function testWhereDateWithInvalidOperator() |
| 310 | + { |
| 311 | + $sql = DB::table('posts')->whereDate('created_at', '? OR 1=1', '2018-01-02'); |
| 312 | + |
| 313 | + PHPUnit::assertArraySubset([ |
| 314 | + [ |
| 315 | + 'column' => 'created_at', |
| 316 | + 'type' => 'Date', |
| 317 | + 'value' => '? OR 1=1', |
| 318 | + 'boolean' => 'and', |
| 319 | + ], |
| 320 | + ], $sql->wheres); |
| 321 | + |
| 322 | + $this->assertSame(0, $sql->count()); |
| 323 | + } |
| 324 | + |
308 | 325 | public function testOrWhereDate() |
309 | 326 | { |
310 | 327 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDate('created_at', '2018-01-02')->count()); |
311 | 328 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDate('created_at', new Carbon('2018-01-02'))->count()); |
312 | 329 | } |
313 | 330 |
|
| 331 | + public function testOrWhereDateWithInvalidOperator() |
| 332 | + { |
| 333 | + $sql = DB::table('posts')->where('id', 1)->orWhereDate('created_at', '? OR 1=1', '2018-01-02'); |
| 334 | + |
| 335 | + PHPUnit::assertArraySubset([ |
| 336 | + [ |
| 337 | + 'column' => 'id', |
| 338 | + 'type' => 'Basic', |
| 339 | + 'value' => 1, |
| 340 | + 'boolean' => 'and', |
| 341 | + ], |
| 342 | + [ |
| 343 | + 'column' => 'created_at', |
| 344 | + 'type' => 'Date', |
| 345 | + 'value' => '? OR 1=1', |
| 346 | + 'boolean' => 'or', |
| 347 | + ], |
| 348 | + ], $sql->wheres); |
| 349 | + |
| 350 | + $this->assertSame(1, $sql->count()); |
| 351 | + } |
| 352 | + |
314 | 353 | public function testWhereDay() |
315 | 354 | { |
316 | 355 | $this->assertSame(1, DB::table('posts')->whereDay('created_at', '02')->count()); |
317 | 356 | $this->assertSame(1, DB::table('posts')->whereDay('created_at', 2)->count()); |
318 | 357 | $this->assertSame(1, DB::table('posts')->whereDay('created_at', new Carbon('2018-01-02'))->count()); |
319 | 358 | } |
320 | 359 |
|
| 360 | + public function testWhereDayWithInvalidOperator() |
| 361 | + { |
| 362 | + $sql = DB::table('posts')->whereDay('created_at', '? OR 1=1', '02'); |
| 363 | + |
| 364 | + PHPUnit::assertArraySubset([ |
| 365 | + [ |
| 366 | + 'column' => 'created_at', |
| 367 | + 'type' => 'Day', |
| 368 | + 'value' => '00', |
| 369 | + 'boolean' => 'and', |
| 370 | + ], |
| 371 | + ], $sql->wheres); |
| 372 | + |
| 373 | + $this->assertSame(0, $sql->count()); |
| 374 | + } |
| 375 | + |
321 | 376 | public function testOrWhereDay() |
322 | 377 | { |
323 | 378 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', '02')->count()); |
324 | 379 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', 2)->count()); |
325 | 380 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereDay('created_at', new Carbon('2018-01-02'))->count()); |
326 | 381 | } |
327 | 382 |
|
| 383 | + public function testOrWhereDayWithInvalidOperator() |
| 384 | + { |
| 385 | + $sql = DB::table('posts')->where('id', 1)->orWhereDay('created_at', '? OR 1=1', '02'); |
| 386 | + |
| 387 | + PHPUnit::assertArraySubset([ |
| 388 | + [ |
| 389 | + 'column' => 'id', |
| 390 | + 'type' => 'Basic', |
| 391 | + 'value' => 1, |
| 392 | + 'boolean' => 'and', |
| 393 | + ], |
| 394 | + [ |
| 395 | + 'column' => 'created_at', |
| 396 | + 'type' => 'Day', |
| 397 | + 'value' => '00', |
| 398 | + 'boolean' => 'or', |
| 399 | + ], |
| 400 | + ], $sql->wheres); |
| 401 | + |
| 402 | + $this->assertSame(1, $sql->count()); |
| 403 | + } |
| 404 | + |
328 | 405 | public function testWhereMonth() |
329 | 406 | { |
330 | 407 | $this->assertSame(1, DB::table('posts')->whereMonth('created_at', '01')->count()); |
331 | 408 | $this->assertSame(1, DB::table('posts')->whereMonth('created_at', 1)->count()); |
332 | 409 | $this->assertSame(1, DB::table('posts')->whereMonth('created_at', new Carbon('2018-01-02'))->count()); |
333 | 410 | } |
334 | 411 |
|
| 412 | + public function testWhereMonthWithInvalidOperator() |
| 413 | + { |
| 414 | + $sql = DB::table('posts')->whereMonth('created_at', '? OR 1=1', '01'); |
| 415 | + |
| 416 | + PHPUnit::assertArraySubset([ |
| 417 | + [ |
| 418 | + 'column' => 'created_at', |
| 419 | + 'type' => 'Month', |
| 420 | + 'value' => '00', |
| 421 | + 'boolean' => 'and', |
| 422 | + ], |
| 423 | + ], $sql->wheres); |
| 424 | + |
| 425 | + $this->assertSame(0, $sql->count()); |
| 426 | + } |
| 427 | + |
335 | 428 | public function testOrWhereMonth() |
336 | 429 | { |
337 | 430 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', '01')->count()); |
338 | 431 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', 1)->count()); |
339 | 432 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereMonth('created_at', new Carbon('2018-01-02'))->count()); |
340 | 433 | } |
341 | 434 |
|
| 435 | + public function testOrWhereMonthWithInvalidOperator() |
| 436 | + { |
| 437 | + $sql = DB::table('posts')->where('id', 1)->orWhereMonth('created_at', '? OR 1=1', '01'); |
| 438 | + |
| 439 | + PHPUnit::assertArraySubset([ |
| 440 | + [ |
| 441 | + 'column' => 'id', |
| 442 | + 'type' => 'Basic', |
| 443 | + 'value' => 1, |
| 444 | + 'boolean' => 'and', |
| 445 | + ], |
| 446 | + [ |
| 447 | + 'column' => 'created_at', |
| 448 | + 'type' => 'Month', |
| 449 | + 'value' => '00', |
| 450 | + 'boolean' => 'or', |
| 451 | + ], |
| 452 | + ], $sql->wheres); |
| 453 | + |
| 454 | + $this->assertSame(1, $sql->count()); |
| 455 | + } |
| 456 | + |
342 | 457 | public function testWhereYear() |
343 | 458 | { |
344 | 459 | $this->assertSame(1, DB::table('posts')->whereYear('created_at', '2018')->count()); |
345 | 460 | $this->assertSame(1, DB::table('posts')->whereYear('created_at', 2018)->count()); |
346 | 461 | $this->assertSame(1, DB::table('posts')->whereYear('created_at', new Carbon('2018-01-02'))->count()); |
347 | 462 | } |
348 | 463 |
|
| 464 | + public function testWhereYearWithInvalidOperator() |
| 465 | + { |
| 466 | + $sql = DB::table('posts')->whereYear('created_at', '? OR 1=1', '2018'); |
| 467 | + |
| 468 | + PHPUnit::assertArraySubset([ |
| 469 | + [ |
| 470 | + 'column' => 'created_at', |
| 471 | + 'type' => 'Year', |
| 472 | + 'value' => '? OR 1=1', |
| 473 | + 'boolean' => 'and', |
| 474 | + ], |
| 475 | + ], $sql->wheres); |
| 476 | + |
| 477 | + $this->assertSame(0, $sql->count()); |
| 478 | + } |
| 479 | + |
349 | 480 | public function testOrWhereYear() |
350 | 481 | { |
351 | 482 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', '2018')->count()); |
352 | 483 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', 2018)->count()); |
353 | 484 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereYear('created_at', new Carbon('2018-01-02'))->count()); |
354 | 485 | } |
355 | 486 |
|
| 487 | + public function testOrWhereYearWithInvalidOperator() |
| 488 | + { |
| 489 | + $sql = DB::table('posts')->where('id', 1)->orWhereYear('created_at', '? OR 1=1', '2018'); |
| 490 | + |
| 491 | + PHPUnit::assertArraySubset([ |
| 492 | + [ |
| 493 | + 'column' => 'id', |
| 494 | + 'type' => 'Basic', |
| 495 | + 'value' => 1, |
| 496 | + 'boolean' => 'and', |
| 497 | + ], |
| 498 | + [ |
| 499 | + 'column' => 'created_at', |
| 500 | + 'type' => 'Year', |
| 501 | + 'value' => '? OR 1=1', |
| 502 | + 'boolean' => 'or', |
| 503 | + ], |
| 504 | + ], $sql->wheres); |
| 505 | + |
| 506 | + $this->assertSame(1, $sql->count()); |
| 507 | + } |
| 508 | + |
356 | 509 | public function testWhereTime() |
357 | 510 | { |
358 | 511 | $this->assertSame(1, DB::table('posts')->whereTime('created_at', '03:04:05')->count()); |
359 | 512 | $this->assertSame(1, DB::table('posts')->whereTime('created_at', new Carbon('2018-01-02 03:04:05'))->count()); |
360 | 513 | } |
361 | 514 |
|
| 515 | + public function testWhereTimeWithInvalidOperator() |
| 516 | + { |
| 517 | + $sql = DB::table('posts')->whereTime('created_at', '? OR 1=1', '03:04:05'); |
| 518 | + |
| 519 | + PHPUnit::assertArraySubset([ |
| 520 | + [ |
| 521 | + 'column' => 'created_at', |
| 522 | + 'type' => 'Time', |
| 523 | + 'value' => '? OR 1=1', |
| 524 | + 'boolean' => 'and', |
| 525 | + ], |
| 526 | + ], $sql->wheres); |
| 527 | + |
| 528 | + $this->assertSame(0, $sql->count()); |
| 529 | + } |
| 530 | + |
362 | 531 | public function testOrWhereTime() |
363 | 532 | { |
364 | 533 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereTime('created_at', '03:04:05')->count()); |
365 | 534 | $this->assertSame(2, DB::table('posts')->where('id', 1)->orWhereTime('created_at', new Carbon('2018-01-02 03:04:05'))->count()); |
366 | 535 | } |
367 | 536 |
|
| 537 | + public function testOrWhereTimeWithInvalidOperator() |
| 538 | + { |
| 539 | + $sql = DB::table('posts')->where('id', 1)->orWhereTime('created_at', '? OR 1=1', '03:04:05'); |
| 540 | + |
| 541 | + PHPUnit::assertArraySubset([ |
| 542 | + [ |
| 543 | + 'column' => 'id', |
| 544 | + 'type' => 'Basic', |
| 545 | + 'value' => 1, |
| 546 | + 'boolean' => 'and', |
| 547 | + ], |
| 548 | + [ |
| 549 | + 'column' => 'created_at', |
| 550 | + 'type' => 'Time', |
| 551 | + 'value' => '? OR 1=1', |
| 552 | + 'boolean' => 'or', |
| 553 | + ], |
| 554 | + ], $sql->wheres); |
| 555 | + |
| 556 | + $this->assertSame(1, $sql->count()); |
| 557 | + } |
| 558 | + |
368 | 559 | public function testWhereNested() |
369 | 560 | { |
370 | 561 | $results = DB::table('posts')->where('content', 'Lorem Ipsum.')->whereNested(function ($query) { |
|
0 commit comments