MysqlDateSqlTest.php in Drupal 9
File
core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php
View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\views\query;
use Drupal\Core\Database\Connection;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\query\MysqlDateSql;
class MysqlDateSqlTest extends UnitTestCase {
protected $database;
public function setUp() : void {
parent::setUp();
$this->database = $this
->prophesize(Connection::class)
->reveal();
}
public function testGetDateField() {
$date_sql = new MysqlDateSql($this->database);
$expected = 'foo.field';
$this
->assertEquals($expected, $date_sql
->getDateField('foo.field', TRUE));
$expected = "DATE_ADD('19700101', INTERVAL foo.field SECOND)";
$this
->assertEquals($expected, $date_sql
->getDateField('foo.field', FALSE));
}
public function testGetDateFormat($field, $format, $expected_format) {
$date_sql = new MysqlDateSql($this->database);
$this
->assertEquals("DATE_FORMAT({$field}, '{$expected_format}')", $date_sql
->getDateFormat($field, $format));
}
public function providerTestGetDateFormat() {
return [
[
'foo.field',
'Y-y-M-m',
'%Y-%y-%b-%m',
],
[
'bar.field',
'n-F D d l',
'%c-%M %a %d %W',
],
[
'baz.bar_field',
'j/W/H-h i s A',
'%e/%v/%H-%h %i %s %p',
],
];
}
public function testSetFieldTimezoneOffset() {
$date_sql = new MysqlDateSql($this->database);
$field = 'foobar.field';
$date_sql
->setFieldTimezoneOffset($field, 42);
$this
->assertEquals("(foobar.field + INTERVAL 42 SECOND)", $field);
}
public function testSetTimezoneOffset() {
$database = $this
->prophesize(Connection::class);
$database
->query("SET @@session.time_zone = '42'")
->shouldBeCalledTimes(1);
$date_sql = new MysqlDateSql($database
->reveal());
$date_sql
->setTimezoneOffset(42);
}
}