/**
  * Set up required mocks for Wordpress controller class
  */
 protected function setUp()
 {
     // Set up Wordpress instance mock
     $this->wordpress = $this->getWordpressMock();
     $this->wordpress->expects($this->any())->method('getContent')->will($this->returnValue('<html><body>My fake Wordpress content</body></html>'));
     $this->wordpress->initialize();
 }
 public function testGlobalVariables()
 {
     // When
     $this->wordpress->initialize();
     // Then
     $this->assertArrayHasKey('wp_test_global1', $GLOBALS);
     $this->assertArrayHasKey('wp_test_global2', $GLOBALS);
     $this->assertFalse(array_key_exists('wp_test_global3', $GLOBALS));
 }
 /**
  * On kernel request method.
  *
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     // Loads Wordpress source code in order to allow use of WordPress functions in Symfony.
     if ('ekino_wordpress_catchall' !== $request->attributes->get('_route')) {
         $this->wordpress->loadWordpress();
     }
     $this->checkAuthentication($request);
 }
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     if (!$response instanceof WordpressResponse || $event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     if (!($wp_query = $this->wordpress->getWpQuery())) {
         return;
     }
     if ($wp_query->is_404()) {
         $response->setStatusCode(404);
     }
 }
 /**
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $response = $event->getResponse();
     if (!$response instanceof WordpressResponse || $event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
         return;
     }
     if (!($wp_query = $this->wordpress->getWpQuery())) {
         return;
     }
     $callback = $this->getHttpHeadersCallback();
     $wpHeaders = (array) call_user_func_array($callback, array($event->getRequest()->getUri()));
     foreach ($wpHeaders as $name => $value) {
         // TODO add cache headers support
         if ($name == 'cache-control') {
             //$response->setCache($value);
             continue;
         }
         $response->headers->set($name, $value);
     }
     if ($wp_query->is_404()) {
         $response->setStatusCode(404);
     }
 }
 /**
  * Test onKernelResponse() & checkAuthentication() methods with a user logged in
  * and Wordpress catchall route is loaded (Wordpress should not be loaded).
  *
  * Should sets user token in security context if session key 'token' exists
  */
 public function testOnKernelRequestUserLoggedAndCatchallRoute()
 {
     // Fake Wordpress application to return a user logged in identifier
     $this->listener->expects($this->any())->method('getWordpressLoggedIdentifier')->will($this->returnValue(1));
     $user = new User();
     $token = new UsernamePasswordToken($user, $user->getPass(), 'secured_area', $user->getRoles());
     // Set up a request mock to give to GetResponseEvent class below
     $session = $this->getMock('\\Symfony\\Component\\HttpFoundation\\Session\\Session');
     $session->expects($this->any())->method('getName')->will($this->returnValue('session_test'));
     $session->expects($this->any())->method('has')->with($this->equalTo('token'))->will($this->returnValue(true));
     $session->expects($this->any())->method('get')->with($this->equalTo('token'))->will($this->returnValue($token));
     $request = new Request();
     $request->setSession($session);
     $request->attributes->set('_route', 'ekino_wordpress_catchall');
     $request->cookies->set($request->getSession()->getName(), true);
     // Ensure Wordpress is loaded as route is not the catch all route.
     $this->wordpress->expects($this->never())->method('loadWordpress');
     $getResponseEvent = new GetResponseEvent($this->getMock('\\Symfony\\Component\\HttpKernel\\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
     // Run onKernelRequest() method
     $this->assertEquals(null, $this->tokenStorage->getToken(), 'Should returns no token');
     $this->listener->onKernelRequest($getResponseEvent);
     $this->assertEquals($token, $this->tokenStorage->getToken(), 'Should returns previous token initialized');
 }
 /**
  * Returns an exception when specified Wordpress directory is not found
  */
 public function testExceptionWhenDirectoryNotFound()
 {
     $this->setExpectedException('InvalidArgumentException');
     $wordpress = new Wordpress($this->getKernelMock(), '/a/path/that/does/not/exists');
     $wordpress->initialize();
 }