View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Query\SelectExtender;
class TaggingTest extends DatabaseTestBase {
public function testHasTag() {
$query = $this->connection
->select('test');
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$this
->assertTrue($query
->hasTag('test'), 'hasTag() returned true.');
$this
->assertFalse($query
->hasTag('other'), 'hasTag() returned false.');
}
public function testHasAllTags() {
$query = $this->connection
->select('test');
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$query
->addTag('other');
$this
->assertTrue($query
->hasAllTags('test', 'other'), 'hasAllTags() returned true.');
$this
->assertFalse($query
->hasAllTags('test', 'stuff'), 'hasAllTags() returned false.');
}
public function testHasAnyTag() {
$query = $this->connection
->select('test');
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$this
->assertTrue($query
->hasAnyTag('test', 'other'), 'hasAnyTag() returned true.');
$this
->assertFalse($query
->hasAnyTag('other', 'stuff'), 'hasAnyTag() returned false.');
}
public function testExtenderHasTag() {
$query = $this->connection
->select('test')
->extend(SelectExtender::class);
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$this
->assertTrue($query
->hasTag('test'), 'hasTag() returned true.');
$this
->assertFalse($query
->hasTag('other'), 'hasTag() returned false.');
}
public function testExtenderHasAllTags() {
$query = $this->connection
->select('test')
->extend(SelectExtender::class);
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$query
->addTag('other');
$this
->assertTrue($query
->hasAllTags('test', 'other'), 'hasAllTags() returned true.');
$this
->assertFalse($query
->hasAllTags('test', 'stuff'), 'hasAllTags() returned false.');
}
public function testExtenderHasAnyTag() {
$query = $this->connection
->select('test')
->extend(SelectExtender::class);
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$query
->addTag('test');
$this
->assertTrue($query
->hasAnyTag('test', 'other'), 'hasAnyTag() returned true.');
$this
->assertFalse($query
->hasAnyTag('other', 'stuff'), 'hasAnyTag() returned false.');
}
public function testMetaData() {
$query = $this->connection
->select('test');
$query
->addField('test', 'name');
$query
->addField('test', 'age', 'age');
$data = [
'a' => 'A',
'b' => 'B',
];
$query
->addMetaData('test', $data);
$return = $query
->getMetaData('test');
$this
->assertEquals($data, $return, 'Correct metadata returned.');
$return = $query
->getMetaData('nothere');
$this
->assertNull($return, 'Non-existent key returned NULL.');
}
}