Newer
Older
Angie Byron
committed
<?php
/**
* @file
Dries Buytaert
committed
* Contains \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest.
Angie Byron
committed
*/
Dries Buytaert
committed
namespace Drupal\Tests\views\Unit\Controller {
Angie Byron
committed
use Drupal\Tests\UnitTestCase;
use Drupal\views\Ajax\ViewAjaxResponse;
use Drupal\views\Controller\ViewAjaxController;
use Symfony\Component\HttpFoundation\Request;
Alex Pott
committed
use Symfony\Component\DependencyInjection\ContainerBuilder;
Angie Byron
committed
/**
* @coversDefaultClass \Drupal\views\Controller\ViewAjaxController
* @group views
Angie Byron
committed
*/
class ViewAjaxControllerTest extends UnitTestCase {
/**
* The mocked view entity storage.
*
catch
committed
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
*/
protected $viewStorage;
/**
* The mocked executable factory.
*
* @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $executableFactory;
/**
* The tested views ajax controller.
*
* @var \Drupal\views\Controller\ViewAjaxController
*/
protected $viewAjaxController;
catch
committed
/**
* The mocked current path.
*
* @var \Drupal\Core\Path\CurrentPathStack|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currentPath;
/**
* The redirect destination.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $redirectDestination;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $renderer;
Alex Pott
committed
/**
* {@inheritdoc}
*/
Angie Byron
committed
protected function setUp() {
catch
committed
$this->viewStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
Angie Byron
committed
$this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
->disableOriginalConstructor()
->getMock();
$this->renderer = $this->getMock('\Drupal\Core\Render\RendererInterface');
$this->renderer->expects($this->any())
->method('render')
->will($this->returnCallback(function(array &$elements) {
$elements['#attached'] = [];
return isset($elements['#markup']) ? $elements['#markup'] : '';
}));
catch
committed
$this->currentPath = $this->getMockBuilder('Drupal\Core\Path\CurrentPathStack')
->disableOriginalConstructor()
->getMock();
$this->redirectDestination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface');
$this->viewAjaxController = new ViewAjaxController($this->viewStorage, $this->executableFactory, $this->renderer, $this->currentPath, $this->redirectDestination);
Alex Pott
committed
$this->renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$container = new ContainerBuilder();
$container->set('renderer', $this->renderer);
\Drupal::setContainer($container);
Angie Byron
committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}
/**
* Tests missing view_name and view_display_id
*
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testMissingViewName() {
$request = new Request();
$this->viewAjaxController->ajaxView($request);
}
/**
* Tests with view_name and view_display_id but not existing view.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testMissingView() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
$this->viewStorage->expects($this->once())
->method('load')
->with('test_view')
->will($this->returnValue(FALSE));
$this->viewAjaxController->ajaxView($request);
}
/**
* Tests a view without having access to it.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function testAccessDeniedView() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
$view = $this->getMockBuilder('Drupal\views\Entity\View')
->disableOriginalConstructor()
->getMock();
$this->viewStorage->expects($this->once())
->method('load')
->with('test_view')
->will($this->returnValue($view));
$executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$executable->expects($this->once())
->method('access')
->will($this->returnValue(FALSE));
Angie Byron
committed
$this->executableFactory->expects($this->once())
Angie Byron
committed
->method('get')
->with($view)
->will($this->returnValue($executable));
$this->viewAjaxController->ajaxView($request);
}
/**
* Tests a valid view without arguments pagers etc.
*/
public function testAjaxView() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
list($view, $executable) = $this->setupValidMocks();
$display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
->disableOriginalConstructor()
->getMock();
// Ensure that the pager element is not set.
$display_handler->expects($this->never())
->method('setOption');
Alex Pott
committed
$display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
->disableOriginalConstructor()
->getMock();
Alex Pott
committed
$display_collection->expects($this->any())
->method('get')
->with('page_1')
->will($this->returnValue($display_handler));
Alex Pott
committed
$executable->displayHandlers = $display_collection;
Angie Byron
committed
$response = $this->viewAjaxController->ajaxView($request);
$this->assertTrue($response instanceof ViewAjaxResponse);
$this->assertSame($response->getView(), $executable);
$this->assertViewResultCommand($response);
}
/**
* Tests a valid view with arguments.
*/
public function testAjaxViewWithArguments() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
$request->request->set('view_args', 'arg1/arg2');
list($view, $executable) = $this->setupValidMocks();
$executable->expects($this->once())
->method('preview')
->with('page_1', array('arg1', 'arg2'));
$response = $this->viewAjaxController->ajaxView($request);
$this->assertTrue($response instanceof ViewAjaxResponse);
catch
committed
$this->assertViewResultCommand($response);
}
/**
* Tests a valid view with arguments.
*/
public function testAjaxViewWithEmptyArguments() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
// Simulate a request that has a second, empty argument.
$request->request->set('view_args', 'arg1/');
list($view, $executable) = $this->setupValidMocks();
$executable->expects($this->once())
->method('preview')
->with('page_1', $this->identicalTo(array('arg1', NULL)));
$response = $this->viewAjaxController->ajaxView($request);
$this->assertTrue($response instanceof ViewAjaxResponse);
$this->assertViewResultCommand($response);
}
/**
* Tests a valid view with a pager.
*/
public function testAjaxViewWithPager() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
Alex Pott
committed
$dom_id = $this->randomMachineName(20);
$request->request->set('view_dom_id', $dom_id);
$request->request->set('pager_element', '0');
list($view, $executable) = $this->setupValidMocks();
$display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
->disableOriginalConstructor()
->getMock();
$display_handler->expects($this->once())
->method('setOption', '0')
->with($this->equalTo('pager_element'));
Alex Pott
committed
$display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
->disableOriginalConstructor()
->getMock();
Alex Pott
committed
$display_collection->expects($this->any())
->method('get')
->with('page_1')
->will($this->returnValue($display_handler));
Alex Pott
committed
$executable->displayHandlers = $display_collection;
$response = $this->viewAjaxController->ajaxView($request);
$this->assertTrue($response instanceof ViewAjaxResponse);
$commands = $this->getCommands($response);
$this->assertEquals('viewsScrollTop', $commands[0]['command']);
$this->assertEquals('.js-view-dom-id-' . $dom_id, $commands[0]['selector']);
$this->assertViewResultCommand($response, 1);
Angie Byron
committed
}
/**
* Sets up a bunch of valid mocks like the view entity and executable.
*/
protected function setupValidMocks() {
$view = $this->getMockBuilder('Drupal\views\Entity\View')
->disableOriginalConstructor()
->getMock();
$this->viewStorage->expects($this->once())
->method('load')
->with('test_view')
->will($this->returnValue($view));
$executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$executable->expects($this->once())
->method('access')
->will($this->returnValue(TRUE));
$executable->expects($this->once())
->method('preview')
->will($this->returnValue(array('#markup' => 'View result')));
Angie Byron
committed
$this->executableFactory->expects($this->once())
Angie Byron
committed
->method('get')
->with($view)
->will($this->returnValue($executable));
return array($view, $executable);
}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Gets the commands entry from the response object.
*
* @param \Drupal\views\Ajax\ViewAjaxResponse $response
* The views ajax response object.
*
* @return mixed
* Returns the commands.
*/
protected function getCommands(ViewAjaxResponse $response) {
$reflection_property = new \ReflectionProperty('Drupal\views\Ajax\ViewAjaxResponse', 'commands');
$reflection_property->setAccessible(TRUE);
$commands = $reflection_property->getValue($response);
return $commands;
}
/**
* Ensures that the main view content command is added.
*
* @param \Drupal\views\Ajax\ViewAjaxResponse $response
* The response object.
* @param int $position
* The position where the view content command is expected.
*/
protected function assertViewResultCommand(ViewAjaxResponse $response, $position = 0) {
$commands = $this->getCommands($response);
$this->assertEquals('insert', $commands[$position]['command']);
$this->assertEquals('View result', $commands[$position]['data']);
}