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

(cherry picked from commit 6add1ca7a3)
8.7.x
Alex Pott 2019-07-18 13:37:21 +01:00
parent 63110a6e80
commit fb20927037
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
4 changed files with 70 additions and 1 deletions

View File

@ -282,7 +282,7 @@ class StatementPrefetch implements \Iterator, StatementInterface {
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)) {

View File

@ -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'] = [

View File

@ -27,6 +27,7 @@ abstract class DatabaseTestBase extends KernelTestBase {
$this->connection = Database::getConnection();
$this->installSchema('database_test', [
'test',
'test_classtype',
'test_people',
'test_people_copy',
'test_one_blob',
@ -100,6 +101,15 @@ abstract class DatabaseTestBase extends KernelTestBase {
])
->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',

View File

@ -80,6 +80,27 @@ class FetchTest extends DatabaseTestBase {
$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.
*/