3 namespace Drupal\Tests\views_ui\Unit;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\Entity\View;
8 use Drupal\views_ui\ViewUI;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
12 * @coversDefaultClass \Drupal\views_ui\ViewUI
15 class ViewUIObjectTest extends UnitTestCase {
18 * Tests entity method decoration.
20 public function testEntityDecoration() {
22 $method_args['setOriginalId'] = [12];
23 $method_args['setStatus'] = [TRUE];
24 $method_args['enforceIsNew'] = [FALSE];
25 $method_args['label'] = [LanguageInterface::LANGCODE_NOT_SPECIFIED];
27 $reflection = new \ReflectionClass('Drupal\Core\Config\Entity\ConfigEntityInterface');
28 $interface_methods = [];
29 foreach ($reflection->getMethods() as $reflection_method) {
30 $interface_methods[] = $reflection_method->getName();
32 // EntityInterface::isNew() is missing from the list of methods, because it
33 // calls id(), which breaks the ->expect($this->once()) call. Call it later.
34 // EntityInterface::isSyncing() is only called during syncing process.
35 // EntityInterface::isUninstalling() is only called during uninstallation
36 // process. EntityInterface::getConfigDependencyName() and
37 // ConfigEntityInterface::calculateDependencies() are only used for
38 // dependency management.
39 if (!in_array($reflection_method->getName(), ['isNew', 'isSyncing', 'isUninstalling', 'getConfigDependencyKey', 'getConfigDependencyName', 'calculateDependencies'])) {
40 if (count($reflection_method->getParameters()) == 0) {
41 $method_args[$reflection_method->getName()] = [];
46 $storage = $this->getMock('Drupal\views\Entity\View', $interface_methods, [[], 'view']);
47 $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
48 ->disableOriginalConstructor()
49 ->setConstructorArgs([$storage])
51 $storage->set('executable', $executable);
53 $view_ui = new ViewUI($storage);
55 foreach ($method_args as $method => $args) {
56 $method_mock = $storage->expects($this->once())
58 foreach ($args as $arg) {
59 $method_mock->with($this->equalTo($arg));
61 call_user_func_array([$view_ui, $method], $args);
64 $storage->expects($this->once())
70 * Tests the isLocked method.
72 public function testIsLocked() {
73 $storage = $this->getMock('Drupal\views\Entity\View', [], [[], 'view']);
74 $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
75 ->disableOriginalConstructor()
76 ->setConstructorArgs([$storage])
78 $storage->set('executable', $executable);
79 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
80 $account->expects($this->exactly(2))
82 ->will($this->returnValue(1));
84 $container = new ContainerBuilder();
85 $container->set('current_user', $account);
86 \Drupal::setContainer($container);
88 $view_ui = new ViewUI($storage);
90 // A view_ui without a lock object is not locked.
91 $this->assertFalse($view_ui->isLocked());
93 // Set the lock object with a different owner than the mocked account above.
97 'updated' => (int) $_SERVER['REQUEST_TIME'],
99 $view_ui->lock = $lock;
100 $this->assertTrue($view_ui->isLocked());
102 // Set a different lock object with the same object as the mocked account.
106 'updated' => (int) $_SERVER['REQUEST_TIME'],
108 $view_ui->lock = $lock;
109 $this->assertFalse($view_ui->isLocked());
113 * Tests serialization of the ViewUI object.
115 public function testSerialization() {
116 // Set a container so the DependencySerializationTrait has it.
117 $container = new ContainerBuilder();
118 \Drupal::setContainer($container);
120 $storage = new View([], 'view');
121 $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
122 ->disableOriginalConstructor()
123 ->setConstructorArgs([$storage])
125 $storage->set('executable', $executable);
127 $view_ui = new ViewUI($storage);
129 // Make sure the executable is returned before serializing.
130 $this->assertInstanceOf('Drupal\views\ViewExecutable', $view_ui->getExecutable());
132 $serialized = serialize($view_ui);
134 // Make sure the ViewExecutable class is not found in the serialized string.
135 $this->assertSame(strpos($serialized, '"Drupal\views\ViewExecutable"'), FALSE);
137 $unserialized = unserialize($serialized);
138 $this->assertInstanceOf('Drupal\views_ui\ViewUI', $unserialized);