*/
class ViewAjaxControllerTest extends UnitTestCase {
+ const USE_AJAX = TRUE;
+ const USE_NO_AJAX = FALSE;
+
/**
* The mocked view entity storage.
*
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');
-
- $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
- ->disableOriginalConstructor()
- ->getMock();
- $display_collection->expects($this->any())
- ->method('get')
- ->with('page_1')
- ->will($this->returnValue($display_handler));
-
- $executable->displayHandlers = $display_collection;
-
$this->redirectDestination->expects($this->atLeastOnce())
->method('set')
->with('/test-page?type=article');
$this->assertViewResultCommand($response);
}
+ /**
+ * Tests a valid view without ajax enabled.
+ */
+ public function testAjaxViewWithoutAjax() {
+ $request = new Request();
+ $request->request->set('view_name', 'test_view');
+ $request->request->set('view_display_id', 'page_1');
+ $request->request->set('view_path', '/test-page');
+ $request->request->set('_wrapper_format', 'ajax');
+ $request->request->set('ajax_page_state', 'drupal.settings[]');
+ $request->request->set('type', 'article');
+
+ $this->setupValidMocks(static::USE_NO_AJAX);
+
+ $this->setExpectedException(AccessDeniedHttpException::class);
+ $this->viewAjaxController->ajaxView($request);
+ }
+
/**
* Tests a valid view with arguments.
*/
/**
* Sets up a bunch of valid mocks like the view entity and executable.
+ *
+ * @param bool $use_ajax
+ * Whether the 'use_ajax' option is set on the view display. Defaults to
+ * using ajax (TRUE).
+ *
+ * @return array
+ * A pair of view storage entity and executable.
*/
- protected function setupValidMocks() {
+ protected function setupValidMocks($use_ajax = self::USE_AJAX) {
$view = $this->getMockBuilder('Drupal\views\Entity\View')
->disableOriginalConstructor()
->getMock();
$executable->expects($this->once())
->method('access')
->will($this->returnValue(TRUE));
- $executable->expects($this->once())
+ $executable->expects($this->any())
+ ->method('setDisplay')
+ ->willReturn(TRUE);
+ $executable->expects($this->atMost(1))
->method('preview')
->will($this->returnValue(['#markup' => 'View result']));
->with($view)
->will($this->returnValue($executable));
+ $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');
+ $display_handler->expects($this->any())
+ ->method('getOption')
+ ->with('use_ajax')
+ ->willReturn($use_ajax);
+
+ $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $display_collection->expects($this->any())
+ ->method('get')
+ ->with('page_1')
+ ->will($this->returnValue($display_handler));
+
+ $executable->display_handler = $display_handler;
+ $executable->displayHandlers = $display_collection;
+
return [$view, $executable];
}