Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / FetchTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 use Drupal\Core\Database\RowCountException;
6 use Drupal\Core\Database\StatementInterface;
7 use Drupal\Tests\system\Functional\Database\FakeRecord;
8
9 /**
10  * Tests the Database system's various fetch capabilities.
11  *
12  * We get timeout errors if we try to run too many tests at once.
13  *
14  * @group Database
15  */
16 class FetchTest extends DatabaseTestBase {
17
18   /**
19    * Confirms that we can fetch a record properly in default object mode.
20    */
21   public function testQueryFetchDefault() {
22     $records = [];
23     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25]);
24     $this->assertTrue($result instanceof StatementInterface, 'Result set is a Drupal statement object.');
25     foreach ($result as $record) {
26       $records[] = $record;
27       $this->assertTrue(is_object($record), 'Record is an object.');
28       $this->assertIdentical($record->name, 'John', '25 year old is John.');
29     }
30
31     $this->assertIdentical(count($records), 1, 'There is only one record.');
32   }
33
34   /**
35    * Confirms that we can fetch a record to an object explicitly.
36    */
37   public function testQueryFetchObject() {
38     $records = [];
39     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_OBJ]);
40     foreach ($result as $record) {
41       $records[] = $record;
42       $this->assertTrue(is_object($record), 'Record is an object.');
43       $this->assertIdentical($record->name, 'John', '25 year old is John.');
44     }
45
46     $this->assertIdentical(count($records), 1, 'There is only one record.');
47   }
48
49   /**
50    * Confirms that we can fetch a record to an associative array explicitly.
51    */
52   public function testQueryFetchArray() {
53     $records = [];
54     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_ASSOC]);
55     foreach ($result as $record) {
56       $records[] = $record;
57       if ($this->assertTrue(is_array($record), 'Record is an array.')) {
58         $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
59       }
60     }
61
62     $this->assertIdentical(count($records), 1, 'There is only one record.');
63   }
64
65   /**
66    * Confirms that we can fetch a record into a new instance of a custom class.
67    *
68    * @see \Drupal\system\Tests\Database\FakeRecord
69    */
70   public function testQueryFetchClass() {
71     $records = [];
72     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => FakeRecord::class]);
73     foreach ($result as $record) {
74       $records[] = $record;
75       if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
76         $this->assertIdentical($record->name, 'John', '25 year old is John.');
77       }
78     }
79
80     $this->assertIdentical(count($records), 1, 'There is only one record.');
81   }
82
83   /**
84    * Confirms that we can fetch a record into an indexed array explicitly.
85    */
86   public function testQueryFetchNum() {
87     $records = [];
88     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_NUM]);
89     foreach ($result as $record) {
90       $records[] = $record;
91       if ($this->assertTrue(is_array($record), 'Record is an array.')) {
92         $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
93       }
94     }
95
96     $this->assertIdentical(count($records), 1, 'There is only one record');
97   }
98
99   /**
100    * Confirms that we can fetch a record into a doubly-keyed array explicitly.
101    */
102   public function testQueryFetchBoth() {
103     $records = [];
104     $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_BOTH]);
105     foreach ($result as $record) {
106       $records[] = $record;
107       if ($this->assertTrue(is_array($record), 'Record is an array.')) {
108         $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
109         $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
110       }
111     }
112
113     $this->assertIdentical(count($records), 1, 'There is only one record.');
114   }
115
116   /**
117    * Confirms that we can fetch all records into an array explicitly.
118    */
119   public function testQueryFetchAllColumn() {
120     $query = db_select('test');
121     $query->addField('test', 'name');
122     $query->orderBy('name');
123     $query_result = $query->execute()->fetchAll(\PDO::FETCH_COLUMN);
124
125     $expected_result = ['George', 'John', 'Paul', 'Ringo'];
126     $this->assertEqual($query_result, $expected_result, 'Returned the correct result.');
127   }
128
129   /**
130    * Confirms that we can fetch an entire column of a result set at once.
131    */
132   public function testQueryFetchCol() {
133     $result = db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25]);
134     $column = $result->fetchCol();
135     $this->assertIdentical(count($column), 3, 'fetchCol() returns the right number of records.');
136
137     $result = db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25]);
138     $i = 0;
139     foreach ($result as $record) {
140       $this->assertIdentical($record->name, $column[$i++], 'Column matches direct access.');
141     }
142   }
143
144   /**
145    * Tests that rowCount() throws exception on SELECT query.
146    */
147   public function testRowCount() {
148     $result = db_query('SELECT name FROM {test}');
149     try {
150       $result->rowCount();
151       $exception = FALSE;
152     }
153     catch (RowCountException $e) {
154       $exception = TRUE;
155     }
156     $this->assertTrue($exception, 'Exception was thrown');
157   }
158
159 }