1<?php
2
3namespace Sabre\DAV\Auth\Backend;
4
5use Sabre\DAV;
6use Sabre\HTTP;
7
8require_once 'Sabre/HTTP/ResponseMock.php';
9
10class AbstractBasicTest extends \PHPUnit_Framework_TestCase {
11
12    function testCheckNoHeaders() {
13
14        $request = new HTTP\Request();
15        $response = new HTTP\Response();
16
17        $backend = new AbstractBasicMock();
18
19        $this->assertFalse(
20            $backend->check($request, $response)[0]
21        );
22
23    }
24
25    function testCheckUnknownUser() {
26
27        $request = HTTP\Sapi::createFromServerArray(array(
28            'PHP_AUTH_USER' => 'username',
29            'PHP_AUTH_PW' => 'wrongpassword',
30        ));
31        $response = new HTTP\Response();
32
33        $backend = new AbstractBasicMock();
34
35        $this->assertFalse(
36            $backend->check($request, $response)[0]
37        );
38
39    }
40
41    function testCheckSuccess() {
42
43        $request = HTTP\Sapi::createFromServerArray(array(
44            'PHP_AUTH_USER' => 'username',
45            'PHP_AUTH_PW' => 'password',
46        ));
47        $response = new HTTP\Response();
48
49        $backend = new AbstractBasicMock();
50        $this->assertEquals(
51            [true, 'principals/username'],
52            $backend->check($request, $response)
53        );
54
55    }
56
57    function testRequireAuth() {
58
59        $request = new HTTP\Request();
60        $response = new HTTP\Response();
61
62        $backend = new AbstractBasicMock();
63        $backend->setRealm('writing unittests on a saturday night');
64        $backend->challenge($request, $response);
65
66        $this->assertEquals(
67            'Basic realm="writing unittests on a saturday night"',
68            $response->getHeader('WWW-Authenticate')
69        );
70
71    }
72
73}
74
75
76class AbstractBasicMock extends AbstractBasic {
77
78    /**
79     * Validates a username and password
80     *
81     * This method should return true or false depending on if login
82     * succeeded.
83     *
84     * @param string $username
85     * @param string $password
86     * @return bool
87     */
88    function validateUserPass($username, $password) {
89
90        return ($username == 'username' && $password == 'password');
91
92    }
93
94}
95