@@ -77,7 +77,10 @@ public function testFactoryRemembersInstances(): void
7777 public function testDataTableState (): void
7878 {
7979 $ datatable = $ this ->createMockDataTable ();
80- $ datatable ->add ('foo ' , TextColumn::class)->setMethod (Request::METHOD_GET );
80+ $ datatable
81+ ->add ('foo ' , TextColumn::class)
82+ ->add ('bar ' , TextColumn::class)
83+ ->setMethod (Request::METHOD_GET );
8184 $ datatable ->handleRequest (Request::create ('/?_dt= ' . $ datatable ->getName ()));
8285 $ state = $ datatable ->getState ();
8386
@@ -91,13 +94,20 @@ public function testDataTableState(): void
9194 $ state ->setStart (5 );
9295 $ state ->setLength (10 );
9396 $ state ->setGlobalSearch ('foo ' );
94- $ state ->setOrderBy ([[0 , 'asc ' ], [1 , 'desc ' ]]);
97+ $ state ->setOrderBy ([
98+ [$ datatable ->getColumn (0 ), 'asc ' ],
99+ [$ datatable ->getColumn (1 ), 'desc0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z ' ], // intentional sql-injection test
100+ ]);
95101 $ state ->setColumnSearch ($ datatable ->getColumn (0 ), 'bar ' );
96102
97103 $ this ->assertSame (5 , $ state ->getStart ());
98104 $ this ->assertSame (10 , $ state ->getLength ());
99105 $ this ->assertSame ('foo ' , $ state ->getGlobalSearch ());
100106 $ this ->assertCount (2 , $ state ->getOrderBy ());
107+ foreach ($ state ->getOrderBy () as $ order ) {
108+ // ensure sql-injection failed
109+ $ this ->assertContains ($ order [1 ], [DataTable::SORT_ASCENDING , DataTable::SORT_DESCENDING ]);
110+ }
101111 $ this ->assertSame ('bar ' , $ state ->getSearchColumns (onlySearchable: false )['foo ' ]['search ' ]);
102112
103113 // Test boundaries
@@ -133,6 +143,24 @@ public function testDataTableStateSearchColumns(): void
133143 $ this ->assertSame ('foo ' , $ searchColumns ['foo ' ]['search ' ]);
134144 }
135145
146+ /**
147+ * If ordering is false, ensure columns are not ordered.
148+ */
149+ public function testDataTablesStateOrdering (): void
150+ {
151+ $ datatable = $ this
152+ ->createMockDataTable (['ordering ' => false ])
153+ ->add ('foo ' , TextColumn::class, ['searchable ' => true ])
154+ ->add ('bar ' , TextColumn::class, ['searchable ' => false ])
155+ ->setMethod (Request::METHOD_GET )
156+ ;
157+ $ datatable ->handleRequest (Request::create ('/?_dt= ' . $ datatable ->getName ()));
158+
159+ $ state = $ datatable ->getState ();
160+ $ state ->addOrderBy ($ datatable ->getColumn (0 ), DataTable::SORT_DESCENDING );
161+ $ this ->assertEmpty ($ state ->getOrderBy ());
162+ }
163+
136164 public function testPostMethod (): void
137165 {
138166 $ datatable = $ this ->createMockDataTable ();
0 commit comments