"Fossies" - the Fresh Open Source Software Archive

Member "dav-4.0.3/tests/Sabre/CardDAV/ValidateVCardTest.php" (10 Jan 2020, 8061 Bytes) of package /linux/www/dav-4.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "ValidateVCardTest.php": 4.0.2_vs_4.0.3.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace Sabre\CardDAV;
    6 
    7 use Sabre\DAV;
    8 use Sabre\DAVACL;
    9 use Sabre\HTTP;
   10 
   11 require_once 'Sabre/HTTP/ResponseMock.php';
   12 
   13 class ValidateVCardTest extends \PHPUnit\Framework\TestCase
   14 {
   15     protected $server;
   16     protected $cardBackend;
   17 
   18     public function setUp()
   19     {
   20         $addressbooks = [
   21             [
   22                 'id' => 'addressbook1',
   23                 'principaluri' => 'principals/admin',
   24                 'uri' => 'addressbook1',
   25             ],
   26         ];
   27 
   28         $this->cardBackend = new Backend\Mock($addressbooks, []);
   29         $principalBackend = new DAVACL\PrincipalBackend\Mock();
   30 
   31         $tree = [
   32             new AddressBookRoot($principalBackend, $this->cardBackend),
   33         ];
   34 
   35         $this->server = new DAV\Server($tree);
   36         $this->server->sapi = new HTTP\SapiMock();
   37         $this->server->debugExceptions = true;
   38 
   39         $plugin = new Plugin();
   40         $this->server->addPlugin($plugin);
   41 
   42         $response = new HTTP\ResponseMock();
   43         $this->server->httpResponse = $response;
   44     }
   45 
   46     public function request(HTTP\Request $request, $expectedStatus = null)
   47     {
   48         $this->server->httpRequest = $request;
   49         $this->server->exec();
   50 
   51         if ($expectedStatus) {
   52             $realStatus = $this->server->httpResponse->getStatus();
   53 
   54             $msg = '';
   55             if ($realStatus !== $expectedStatus) {
   56                 $msg = 'Response body: '.$this->server->httpResponse->getBodyAsString();
   57             }
   58             $this->assertEquals(
   59                 $expectedStatus,
   60                 $realStatus,
   61                 $msg
   62             );
   63         }
   64 
   65         return $this->server->httpResponse;
   66     }
   67 
   68     public function testCreateFile()
   69     {
   70         $request = HTTP\Sapi::createFromServerArray([
   71             'REQUEST_METHOD' => 'PUT',
   72             'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
   73         ]);
   74 
   75         $response = $this->request($request);
   76 
   77         $this->assertEquals(415, $response->status);
   78     }
   79 
   80     public function testCreateFileValid()
   81     {
   82         $request = new HTTP\Request(
   83             'PUT',
   84             '/addressbooks/admin/addressbook1/blabla.vcf'
   85         );
   86 
   87         $vcard = <<<VCF
   88 BEGIN:VCARD
   89 VERSION:4.0
   90 UID:foo
   91 FN:Firstname LastName
   92 N:LastName;FirstName;;;
   93 END:VCARD
   94 VCF;
   95         $request->setBody($vcard);
   96 
   97         $response = $this->request($request, 201);
   98 
   99         // The custom Ew header should not be set
  100         $this->assertNull(
  101             $response->getHeader('X-Sabre-Ew-Gross')
  102         );
  103         // Valid, non-auto-fixed responses should contain an ETag.
  104         $this->assertTrue(
  105             null !== $response->getHeader('ETag'),
  106             'We did not receive an etag'
  107         );
  108 
  109         $expected = [
  110             'uri' => 'blabla.vcf',
  111             'carddata' => $vcard,
  112             'size' => strlen($vcard),
  113             'etag' => '"'.md5($vcard).'"',
  114         ];
  115 
  116         $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
  117     }
  118 
  119     /**
  120      * This test creates an intentionally broken vCard that vobject is able
  121      * to automatically repair.
  122      *
  123      * @depends testCreateFileValid
  124      */
  125     public function testCreateVCardAutoFix()
  126     {
  127         $request = new HTTP\Request(
  128             'PUT',
  129             '/addressbooks/admin/addressbook1/blabla.vcf'
  130         );
  131 
  132         // The error in this vcard is that there's not enough semi-colons in N
  133         $vcard = <<<VCF
  134 BEGIN:VCARD
  135 VERSION:4.0
  136 UID:foo
  137 FN:Firstname LastName
  138 N:LastName;FirstName;;
  139 END:VCARD
  140 VCF;
  141 
  142         $request->setBody($vcard);
  143 
  144         $response = $this->request($request, 201);
  145 
  146         // Auto-fixed vcards should NOT return an etag
  147         $this->assertNull(
  148             $response->getHeader('ETag')
  149         );
  150 
  151         // We should have gotten an Ew header
  152         $this->assertNotNull(
  153             $response->getHeader('X-Sabre-Ew-Gross')
  154         );
  155 
  156         $expectedVCard = <<<VCF
  157 BEGIN:VCARD\r
  158 VERSION:4.0\r
  159 UID:foo\r
  160 FN:Firstname LastName\r
  161 N:LastName;FirstName;;;\r
  162 END:VCARD\r
  163 
  164 VCF;
  165 
  166         $expected = [
  167             'uri' => 'blabla.vcf',
  168             'carddata' => $expectedVCard,
  169             'size' => strlen($expectedVCard),
  170             'etag' => '"'.md5($expectedVCard).'"',
  171         ];
  172 
  173         $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
  174     }
  175 
  176     /**
  177      * This test creates an intentionally broken vCard that vobject is able
  178      * to automatically repair.
  179      *
  180      * However, we're supplying a heading asking the server to treat the
  181      * request as strict, so the server should still let the request fail.
  182      *
  183      * @depends testCreateFileValid
  184      */
  185     public function testCreateVCardStrictFail()
  186     {
  187         $request = new HTTP\Request(
  188             'PUT',
  189             '/addressbooks/admin/addressbook1/blabla.vcf',
  190             [
  191                 'Prefer' => 'handling=strict',
  192             ]
  193         );
  194 
  195         // The error in this vcard is that there's not enough semi-colons in N
  196         $vcard = <<<VCF
  197 BEGIN:VCARD
  198 VERSION:4.0
  199 UID:foo
  200 FN:Firstname LastName
  201 N:LastName;FirstName;;
  202 END:VCARD
  203 VCF;
  204 
  205         $request->setBody($vcard);
  206         $this->request($request, 415);
  207     }
  208 
  209     public function testCreateFileNoUID()
  210     {
  211         $request = new HTTP\Request(
  212             'PUT',
  213             '/addressbooks/admin/addressbook1/blabla.vcf'
  214         );
  215         $vcard = <<<VCF
  216 BEGIN:VCARD
  217 VERSION:4.0
  218 FN:Firstname LastName
  219 N:LastName;FirstName;;;
  220 END:VCARD
  221 VCF;
  222         $request->setBody($vcard);
  223 
  224         $response = $this->request($request, 201);
  225 
  226         $foo = $this->cardBackend->getCard('addressbook1', 'blabla.vcf');
  227         $this->assertTrue(
  228             false !== strpos($foo['carddata'], 'UID'),
  229             print_r($foo, true)
  230         );
  231     }
  232 
  233     public function testCreateFileJson()
  234     {
  235         $request = new HTTP\Request(
  236             'PUT',
  237             '/addressbooks/admin/addressbook1/blabla.vcf'
  238         );
  239         $request->setBody('[ "vcard" , [ [ "VERSION", {}, "text", "4.0"], [ "UID" , {}, "text", "foo" ], [ "FN", {}, "text", "FirstName LastName"] ] ]');
  240 
  241         $response = $this->request($request);
  242 
  243         $this->assertEquals(201, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  244 
  245         $foo = $this->cardBackend->getCard('addressbook1', 'blabla.vcf');
  246         $this->assertEquals("BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nFN:FirstName LastName\r\nEND:VCARD\r\n", $foo['carddata']);
  247     }
  248 
  249     public function testCreateFileVCalendar()
  250     {
  251         $request = HTTP\Sapi::createFromServerArray([
  252             'REQUEST_METHOD' => 'PUT',
  253             'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf',
  254         ]);
  255         $request->setBody("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n");
  256 
  257         $response = $this->request($request);
  258 
  259         $this->assertEquals(415, $response->status, 'Incorrect status returned! Full response body: '.$response->getBodyAsString());
  260     }
  261 
  262     public function testUpdateFile()
  263     {
  264         $this->cardBackend->createCard('addressbook1', 'blabla.vcf', 'foo');
  265         $request = new HTTP\Request(
  266             'PUT',
  267             '/addressbooks/admin/addressbook1/blabla.vcf'
  268         );
  269 
  270         $response = $this->request($request, 415);
  271     }
  272 
  273     public function testUpdateFileParsableBody()
  274     {
  275         $this->cardBackend->createCard('addressbook1', 'blabla.vcf', 'foo');
  276         $request = new HTTP\Request(
  277             'PUT',
  278             '/addressbooks/admin/addressbook1/blabla.vcf'
  279         );
  280 
  281         $body = "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:foo\r\nFN:FirstName LastName\r\nEND:VCARD\r\n";
  282         $request->setBody($body);
  283 
  284         $response = $this->request($request, 204);
  285 
  286         $expected = [
  287             'uri' => 'blabla.vcf',
  288             'carddata' => $body,
  289             'size' => strlen($body),
  290             'etag' => '"'.md5($body).'"',
  291         ];
  292 
  293         $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf'));
  294     }
  295 }