3 namespace Drupal\Tests\Core\Database;
5 use Drupal\Core\Database\Query\Select;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the orderBy() method of select queries.
13 class OrderByTest extends UnitTestCase {
16 * The select query object to test.
18 * @var \Drupal\Core\Database\Query\Select
25 protected function setUp() {
26 $connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
27 ->disableOriginalConstructor()
28 ->getMockForAbstractClass();
29 $this->query = new Select('test', NULL, $connection);
33 * Checks that invalid sort directions in ORDER BY get converted to ASC.
35 public function testInvalidDirection() {
36 $this->query->orderBy('test', 'invalid direction');
37 $order_bys = $this->query->getOrderBy();
38 $this->assertEquals($order_bys['test'], 'ASC', 'Invalid order by direction is converted to ASC.');
42 * Tests that fields passed for ordering get escaped properly.
44 public function testFieldEscaping() {
45 $this->query->orderBy('x; DROP table node; --');
46 $sql = $this->query->__toString();
47 $this->assertStringEndsWith('ORDER BY xDROPtablenode ASC', $sql, 'Order by field is escaped correctly.');