0

I'm trying to hit an endpoint which shows different data when logged in/out. I am trying to write a unit test, but can't seem to figure out how to hit the end point with user context. How can I do this?

Here is my code so far:

/**
* Tests to make sure logged out users cant access file ajax.
*/
 public function testAccessDeniedLoggedOut() {
 $response = file_get_contents($this->fileAjaxUrl);
 $this->assertTrue(strpos($response, $this->accessDeniedMessage) !== FALSE);
 }

/**
* Tests to make sure a logged user can access file ajax.
*/
public function testAccessAllowedLoggedIn() {
 $form_state = array('uid' => 1);
 user_login_submit(array(), $form_state);

 global $user;
 $response = file_get_contents($this->fileAjaxUrl);
 $response = drupal_http_request($this->fileAjaxUrl);
 $this->assertTrue(strpos($response, $this->accessAllowedMessage) !== FALSE);
}
2
  • You need to login, get the session cookie, then pass that along to the subsequent request
    – Clive
    Aug 21, 2018 at 8:21
  • @clive Do you have a good example of how to do that? Aug 22, 2018 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.