Backup of db before drupal security update
[yaffs-website] / web / core / modules / views_ui / tests / src / Unit / ViewUIObjectTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Unit;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\views_ui\ViewUI
13  * @group views_ui
14  */
15 class ViewUIObjectTest extends UnitTestCase {
16
17   /**
18    * Tests entity method decoration.
19    */
20   public function testEntityDecoration() {
21     $method_args = [];
22     $method_args['setOriginalId'] = [12];
23     $method_args['setStatus'] = [TRUE];
24     $method_args['enforceIsNew'] = [FALSE];
25     $method_args['label'] = [LanguageInterface::LANGCODE_NOT_SPECIFIED];
26
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();
31
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()] = [];
42         }
43       }
44     }
45
46     $storage = $this->getMock('Drupal\views\Entity\View', $interface_methods, [[], 'view']);
47     $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
48       ->disableOriginalConstructor()
49       ->setConstructorArgs([$storage])
50       ->getMock();
51     $storage->set('executable', $executable);
52
53     $view_ui = new ViewUI($storage);
54
55     foreach ($method_args as $method => $args) {
56       $method_mock = $storage->expects($this->once())
57         ->method($method);
58       foreach ($args as $arg) {
59         $method_mock->with($this->equalTo($arg));
60       }
61       call_user_func_array([$view_ui, $method], $args);
62     }
63
64     $storage->expects($this->once())
65       ->method('isNew');
66     $view_ui->isNew();
67   }
68
69   /**
70    * Tests the isLocked method.
71    */
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])
77       ->getMock();
78     $storage->set('executable', $executable);
79     $account = $this->getMock('Drupal\Core\Session\AccountInterface');
80     $account->expects($this->exactly(2))
81       ->method('id')
82       ->will($this->returnValue(1));
83
84     $container = new ContainerBuilder();
85     $container->set('current_user', $account);
86     \Drupal::setContainer($container);
87
88     $view_ui = new ViewUI($storage);
89
90     // A view_ui without a lock object is not locked.
91     $this->assertFalse($view_ui->isLocked());
92
93     // Set the lock object with a different owner than the mocked account above.
94     $lock = (object) [
95       'owner' => 2,
96       'data' => [],
97       'updated' => (int) $_SERVER['REQUEST_TIME'],
98     ];
99     $view_ui->lock = $lock;
100     $this->assertTrue($view_ui->isLocked());
101
102     // Set a different lock object with the same object as the mocked account.
103     $lock = (object) [
104       'owner' => 1,
105       'data' => [],
106       'updated' => (int) $_SERVER['REQUEST_TIME'],
107     ];
108     $view_ui->lock = $lock;
109     $this->assertFalse($view_ui->isLocked());
110   }
111
112   /**
113    * Tests serialization of the ViewUI object.
114    */
115   public function testSerialization() {
116     // Set a container so the DependencySerializationTrait has it.
117     $container = new ContainerBuilder();
118     \Drupal::setContainer($container);
119
120     $storage = new View([], 'view');
121     $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
122       ->disableOriginalConstructor()
123       ->setConstructorArgs([$storage])
124       ->getMock();
125     $storage->set('executable', $executable);
126
127     $view_ui = new ViewUI($storage);
128
129     // Make sure the executable is returned before serializing.
130     $this->assertInstanceOf('Drupal\views\ViewExecutable', $view_ui->getExecutable());
131
132     $serialized = serialize($view_ui);
133
134     // Make sure the ViewExecutable class is not found in the serialized string.
135     $this->assertSame(strpos($serialized, '"Drupal\views\ViewExecutable"'), FALSE);
136
137     $unserialized = unserialize($serialized);
138     $this->assertInstanceOf('Drupal\views_ui\ViewUI', $unserialized);
139   }
140
141 }