Skip to content
Snippets Groups Projects
Unverified Commit 6add1ca7 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1476782 by daffie, droplet, donquixote, Katiemouse, Lendude, lnunesbr,...

Issue #1476782 by daffie, droplet, donquixote, Katiemouse, Lendude, lnunesbr, ekl1773, init90, jhedstrom, catch, clemens.tolboom, amateescu: DatabaseStatementPrefetch::current PHP function array_unshift() are used incorrectly
parent 037ab215
No related branches found
No related tags found
No related merge requests found
......@@ -282,7 +282,7 @@ public function current() {
case \PDO::FETCH_OBJ:
return (object) $this->currentRow;
case \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE:
$class_name = array_unshift($this->currentRow);
$class_name = array_shift($this->currentRow);
// Deliberate no break.
case \PDO::FETCH_CLASS:
if (!isset($class_name)) {
......
......@@ -54,6 +54,44 @@ function database_test_schema() {
],
];
$schema['test_classtype'] = [
'description' => 'A duplicate version of the test table, used for fetch_style PDO::FETCH_CLASSTYPE tests.',
'fields' => [
'classname' => [
'description' => "A custom class name",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => "A person's name",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'age' => [
'description' => "The person's age",
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'job' => [
'description' => "The person's job",
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
],
'primary key' => ['job'],
'indexes' => [
'ages' => ['age'],
],
];
// This is an alternate version of the same table that is structured the same
// but has a non-serial Primary Key.
$schema['test_people'] = [
......
......@@ -27,6 +27,7 @@ protected function setUp() {
$this->connection = Database::getConnection();
$this->installSchema('database_test', [
'test',
'test_classtype',
'test_people',
'test_people_copy',
'test_one_blob',
......@@ -100,6 +101,15 @@ public static function addSampleData() {
])
->execute();
$connection->insert('test_classtype')
->fields([
'classname' => 'Drupal\Tests\system\Functional\Database\FakeRecord',
'name' => 'Kay',
'age' => 26,
'job' => 'Web Developer',
])
->execute();
$connection->insert('test_people')
->fields([
'name' => 'Meredith',
......
......@@ -80,6 +80,27 @@ public function testQueryFetchClass() {
$this->assertIdentical(count($records), 1, 'There is only one record.');
}
/**
* Confirms that we can fetch a record into a new instance of a custom class.
* The name of the class is determined from a value of the first column.
*
* @see \Drupal\Tests\system\Functional\Database\FakeRecord
*/
public function testQueryFetchClasstype() {
$records = [];
$result = $this->connection->query('SELECT classname, name, job FROM {test_classtype} WHERE age = :age', [':age' => 26], ['fetch' => \PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE]);
foreach ($result as $record) {
$records[] = $record;
if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
$this->assertSame('Kay', $record->name, 'Kay is found.');
$this->assertSame('Web Developer', $record->job, 'A 26 year old Web Developer.');
}
$this->assertFalse(isset($record->classname), 'Classname field not found, as intended.');
}
$this->assertCount(1, $records, 'There is only one record.');
}
/**
* Confirms that we can fetch a record into an indexed array explicitly.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment