e7cd98477b33a106e689d952185f04a6fb76556e
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / Block / ViewsBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\Block;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\Plugin\Block\ViewsBlock;
8
9 /**
10  * @coversDefaultClass \Drupal\views\Plugin\block\ViewsBlock
11  * @group views
12  */
13 class ViewsBlockTest extends UnitTestCase {
14
15   /**
16    * The view executable.
17    *
18    * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $executable;
21
22   /**
23    * The view executable factory.
24    *
25    * @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $executableFactory;
28
29   /**
30    * The view entity.
31    *
32    * @var \Drupal\views\ViewEntityInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $view;
35
36   /**
37    * The view storage.
38    *
39    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
40    */
41   protected $storage;
42
43   /**
44    * The mocked user account.
45    *
46    * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $account;
49
50   /**
51    * The mocked display handler.
52    *
53    * @var \Drupal\views\Plugin\views\display\Block|\PHPUnit_Framework_MockObject_MockObject
54    */
55   protected $displayHandler;
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function setUp() {
61     // TODO: Change the autogenerated stub.
62     parent::setUp();
63     $condition_plugin_manager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
64     $condition_plugin_manager->expects($this->any())
65       ->method('getDefinitions')
66       ->will($this->returnValue([]));
67     $container = new ContainerBuilder();
68     $container->set('plugin.manager.condition', $condition_plugin_manager);
69     \Drupal::setContainer($container);
70
71     $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
72       ->disableOriginalConstructor()
73       ->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage', 'getShowAdminLinks'])
74       ->getMock();
75     $this->executable->expects($this->any())
76       ->method('setDisplay')
77       ->with('block_1')
78       ->will($this->returnValue(TRUE));
79     $this->executable->expects($this->any())
80       ->method('getShowAdminLinks')
81       ->willReturn(FALSE);
82
83     $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
84       ->disableOriginalConstructor()
85       ->setMethods(NULL)
86       ->getMock();
87
88     $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
89       ->disableOriginalConstructor()
90       ->getMock();
91     $this->view->expects($this->any())
92       ->method('id')
93       ->willReturn('test_view');
94     $this->executable->storage = $this->view;
95
96     $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
97       ->disableOriginalConstructor()
98       ->getMock();
99     $this->executableFactory->expects($this->any())
100       ->method('get')
101       ->with($this->view)
102       ->will($this->returnValue($this->executable));
103
104     $this->displayHandler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
105       ->disableOriginalConstructor()
106       ->getMock();
107
108     $this->displayHandler->expects($this->any())
109       ->method('blockSettings')
110       ->willReturn([]);
111
112     $this->displayHandler->expects($this->any())
113       ->method('getPluginId')
114       ->willReturn('block');
115
116     $this->displayHandler->expects($this->any())
117       ->method('getHandlers')
118       ->willReturn([]);
119
120     $this->executable->display_handler = $this->displayHandler;
121
122     $this->storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
123       ->disableOriginalConstructor()
124       ->getMock();
125
126     $this->storage->expects($this->any())
127       ->method('load')
128       ->with('test_view')
129       ->will($this->returnValue($this->view));
130     $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
131   }
132
133   /**
134    * Tests the build method.
135    *
136    * @see \Drupal\views\Plugin\block\ViewsBlock::build()
137    */
138   public function testBuild() {
139     $output = $this->randomMachineName(100);
140     $build = ['view_build' => $output, '#view_id' => 'test_view', '#view_display_plugin_class' => '\Drupal\views\Plugin\views\display\Block', '#view_display_show_admin_links' => FALSE, '#view_display_plugin_id' => 'block', '#pre_rendered' => TRUE];
141     $this->executable->expects($this->once())
142       ->method('buildRenderable')
143       ->with('block_1', [])
144       ->willReturn($build);
145
146     $block_id = 'views_block:test_view-block_1';
147     $config = [];
148     $definition = [];
149
150     $definition['provider'] = 'views';
151     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
152
153     $this->assertEquals($build, $plugin->build());
154   }
155
156   /**
157    * Tests the build method.
158    *
159    * @covers ::build
160    */
161   public function testBuildEmpty() {
162     $build = ['view_build' => [], '#view_id' => 'test_view', '#view_display_plugin_class' => '\Drupal\views\Plugin\views\display\Block', '#view_display_show_admin_links' => FALSE, '#view_display_plugin_id' => 'block', '#pre_rendered' => TRUE, '#cache' => ['contexts' => ['user']]];
163     $this->executable->expects($this->once())
164       ->method('buildRenderable')
165       ->with('block_1', [])
166       ->willReturn($build);
167
168     $block_id = 'views_block:test_view-block_1';
169     $config = [];
170     $definition = [];
171
172     $definition['provider'] = 'views';
173     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
174
175     $this->assertEquals(array_intersect_key($build, ['#cache' => TRUE]), $plugin->build());
176   }
177
178   /**
179    * Tests the build method with a failed execution.
180    *
181    * @see \Drupal\views\Plugin\block\ViewsBlock::build()
182    */
183   public function testBuildFailed() {
184     $output = FALSE;
185     $this->executable->expects($this->once())
186       ->method('buildRenderable')
187       ->with('block_1', [])
188       ->willReturn($output);
189
190     $block_id = 'views_block:test_view-block_1';
191     $config = [];
192     $definition = [];
193
194     $definition['provider'] = 'views';
195     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
196
197     $this->assertEquals([], $plugin->build());
198   }
199
200 }
201
202 // @todo https://www.drupal.org/node/2571679 replace
203 //   views_add_contextual_links().
204 namespace Drupal\views\Plugin\Block;
205
206 if (!function_exists('views_add_contextual_links')) {
207
208   function views_add_contextual_links() {
209   }
210
211 }