View source
<?php
namespace Drupal\Tests\views_ui\Unit;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TempStore\Lock;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Entity\View;
use Drupal\views_ui\ViewUI;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ViewUIObjectTest extends UnitTestCase {
public function testEntityDecoration() {
$method_args = [];
$method_args['setOriginalId'] = [
12,
];
$method_args['setStatus'] = [
TRUE,
];
$method_args['enforceIsNew'] = [
FALSE,
];
$method_args['label'] = [
LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$reflection = new \ReflectionClass('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$interface_methods = [];
foreach ($reflection
->getMethods() as $reflection_method) {
$interface_methods[] = $reflection_method
->getName();
if (!in_array($reflection_method
->getName(), [
'isNew',
'isSyncing',
'isUninstalling',
'getConfigDependencyKey',
'getConfigDependencyName',
'calculateDependencies',
])) {
if (count($reflection_method
->getParameters()) == 0) {
$method_args[$reflection_method
->getName()] = [];
}
}
}
$storage = $this
->getMockBuilder('Drupal\\views\\Entity\\View')
->setMethods($interface_methods)
->setConstructorArgs([
[],
'view',
])
->getMock();
$executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->setConstructorArgs([
$storage,
])
->getMock();
$storage
->set('executable', $executable);
$view_ui = new ViewUI($storage);
foreach ($method_args as $method => $args) {
$method_mock = $storage
->expects($this
->once())
->method($method);
foreach ($args as $arg) {
$method_mock
->with($this
->equalTo($arg));
}
call_user_func_array([
$view_ui,
$method,
], $args);
}
$storage
->expects($this
->once())
->method('isNew');
$view_ui
->isNew();
}
public function testIsLocked() {
$storage = $this
->getMockBuilder('Drupal\\views\\Entity\\View')
->setConstructorArgs([
[],
'view',
])
->getMock();
$executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->setConstructorArgs([
$storage,
])
->getMock();
$storage
->set('executable', $executable);
$account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$account
->expects($this
->exactly(2))
->method('id')
->will($this
->returnValue(1));
$container = new ContainerBuilder();
$container
->set('current_user', $account);
\Drupal::setContainer($container);
$view_ui = new ViewUI($storage);
$this
->assertFalse($view_ui
->isLocked());
$lock = new Lock(2, (int) $_SERVER['REQUEST_TIME']);
$view_ui
->setLock($lock);
$this
->assertTrue($view_ui
->isLocked());
$lock = new Lock(1, (int) $_SERVER['REQUEST_TIME']);
$view_ui
->setLock($lock);
$this
->assertFalse($view_ui
->isLocked());
$view_ui
->unsetLock(NULL);
$this
->assertFalse($view_ui
->isLocked());
}
public function testSerialization() {
$container = new ContainerBuilder();
\Drupal::setContainer($container);
$storage = new View([], 'view');
$executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->setConstructorArgs([
$storage,
])
->getMock();
$storage
->set('executable', $executable);
$view_ui = new ViewUI($storage);
$this
->assertInstanceOf('Drupal\\views\\ViewExecutable', $view_ui
->getExecutable());
$serialized = serialize($view_ui);
$this
->assertStringNotContainsString('"Drupal\\views\\ViewExecutable"', $serialized);
$unserialized = unserialize($serialized);
$this
->assertInstanceOf('Drupal\\views_ui\\ViewUI', $unserialized);
}
}