I am trying to make tests for the blog tutorial in the cookbook. Recently 
I've been stuck and trying to solve my problem with testing as authed users.

Here is my test code:

    /**
     * Tests wheter you can edit article with unauthorized user.
     *
     * @return void
     */
    public function testEditUnauthorized()
    {
        // Mock the articles controller with Auth.
        $articles = $this->generate("Articles", ["components" => ["Auth" => 
["user"]]]);

        // Set the user to be logged in as one from the fixture.
        $user = ["id" => 2, "username" => "Jack", "role" => "author"];

        // Make Auth's "user" method return the user specified.
        $articles->Auth
            ->expects($this->once())
            ->method("user")
            ->with(null)
            ->willReturn($user);

        // Test the action on edit function on someone elses article.
        $this->testAction("/articles/edit/1");

        // This shouldn't go to the article edit.
        $this->assertNotEquals("/articles/edit/1", $this->headers["Location"
]);

        // (Test not yet implemented fully).
        $this->markTestIncomplete('testEditUnauthorized not implemented.');
    }

So, the above test should try to edit someone else's article as 'Jack' who 
has role author, which should be denied. This is checked with 
assertNotEquals(). But running this test gives me error:

*Fatal error: Call to a member function delete() on a non-object in 
C:\...\cakephp\src\Controller\Component\AuthComponent.php on line 660.*
The code there, at the line 660 is underlined in the following:

/**
 * Similar to AuthComponent::user() except if the session user cannot be 
found, connected authentication
 * objects will have their getUser() methods called. This lets stateless 
authentication methods function correctly.
 *
 * @return bool true if a user can be found, false if one cannot.
 */
    protected function _getUser() {
        $user = $this->user();
        if ($user) {
            *$this->session->delete('Auth.redirect');* // (Line 660)
            return true;
        }

        if (empty($this->_authenticateObjects)) {
            $this->constructAuthenticate();
        }
        foreach ($this->_authenticateObjects as $auth) {
            $result = $auth->getUser($this->request);
            if (!empty($result) && is_array($result)) {
                $this->_user = $result;
                return true;
            }
        }

        return false;
    }

So, it has something to do with sessions not working. I run the PHPUnit 
also with --stderr flag, so that is not the case. I also tried adding 
"Session" to the components like this:

$articles = $this->generate("Articles", ["components" => ["Auth" => ["user"
], "Session"]]);

but this didn't help either and I get the same error message. Any idea what 
might be causing this?

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to