|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Solid Client PHP project. |
| 5 | + * (c) Kévin Dunglas <kevin@dunglas.fr> |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Dunglas\PhpSolidClient\Tests; |
| 13 | + |
| 14 | +use Dunglas\PhpSolidClient\SolidClient; |
| 15 | +use Dunglas\PhpSolidClient\Wac\AclDocument; |
| 16 | +use Dunglas\PhpSolidClient\Wac\Authorization; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 19 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 20 | + |
| 21 | +class WacTest extends TestCase |
| 22 | +{ |
| 23 | + public function testAuthorizationConstants(): void |
| 24 | + { |
| 25 | + $this->assertSame('http://www.w3.org/ns/auth/acl#Read', Authorization::MODE_READ); |
| 26 | + $this->assertSame('http://www.w3.org/ns/auth/acl#Write', Authorization::MODE_WRITE); |
| 27 | + $this->assertSame('http://www.w3.org/ns/auth/acl#Append', Authorization::MODE_APPEND); |
| 28 | + $this->assertSame('http://www.w3.org/ns/auth/acl#Control', Authorization::MODE_CONTROL); |
| 29 | + } |
| 30 | + |
| 31 | + public function testAclDocumentToTurtle(): void |
| 32 | + { |
| 33 | + $acl = new AclDocument('http://pod.example/resource.acl', [ |
| 34 | + new Authorization( |
| 35 | + 'http://pod.example/resource', |
| 36 | + ['http://example.com/user#me'], |
| 37 | + [], |
| 38 | + [Authorization::MODE_READ, Authorization::MODE_WRITE, Authorization::MODE_CONTROL], |
| 39 | + ), |
| 40 | + new Authorization( |
| 41 | + 'http://pod.example/resource', |
| 42 | + [], |
| 43 | + [Authorization::AGENT_CLASS_PUBLIC], |
| 44 | + [Authorization::MODE_READ], |
| 45 | + ), |
| 46 | + ]); |
| 47 | + |
| 48 | + $turtle = $acl->toTurtle(); |
| 49 | + |
| 50 | + $this->assertStringContainsString('@prefix acl:', $turtle); |
| 51 | + $this->assertStringContainsString('acl:agent <http://example.com/user#me>', $turtle); |
| 52 | + $this->assertStringContainsString('acl:agentClass foaf:Agent', $turtle); |
| 53 | + $this->assertStringContainsString('acl:mode acl:Read', $turtle); |
| 54 | + $this->assertStringContainsString('acl:mode acl:Write', $turtle); |
| 55 | + $this->assertStringContainsString('acl:mode acl:Control', $turtle); |
| 56 | + $this->assertStringContainsString('acl:accessTo <http://pod.example/resource>', $turtle); |
| 57 | + } |
| 58 | + |
| 59 | + public function testAclDocumentFromTurtle(): void |
| 60 | + { |
| 61 | + $turtle = <<<'TURTLE' |
| 62 | +@prefix acl: <http://www.w3.org/ns/auth/acl#>. |
| 63 | +@prefix foaf: <http://xmlns.com/foaf/0.1/>. |
| 64 | +
|
| 65 | +<#owner> |
| 66 | + a acl:Authorization; |
| 67 | + acl:agent <http://example.com/user#me>; |
| 68 | + acl:mode acl:Read, acl:Write, acl:Control; |
| 69 | + acl:accessTo <http://pod.example/resource>; |
| 70 | + acl:default <http://pod.example/resource>. |
| 71 | +
|
| 72 | +<#public> |
| 73 | + a acl:Authorization; |
| 74 | + acl:agentClass foaf:Agent; |
| 75 | + acl:mode acl:Read; |
| 76 | + acl:accessTo <http://pod.example/resource>. |
| 77 | +TURTLE; |
| 78 | + |
| 79 | + $acl = AclDocument::fromTurtle($turtle, 'http://pod.example/resource.acl', 'http://pod.example/resource'); |
| 80 | + |
| 81 | + $this->assertSame('http://pod.example/resource.acl', $acl->aclUrl); |
| 82 | + $this->assertCount(2, $acl->authorizations); |
| 83 | + |
| 84 | + $owner = $acl->authorizations[0]; |
| 85 | + $this->assertSame(['http://example.com/user#me'], $owner->agents); |
| 86 | + $this->assertContains(Authorization::MODE_READ, $owner->modes); |
| 87 | + $this->assertContains(Authorization::MODE_WRITE, $owner->modes); |
| 88 | + $this->assertContains(Authorization::MODE_CONTROL, $owner->modes); |
| 89 | + $this->assertTrue($owner->isDefault); |
| 90 | + |
| 91 | + $public = $acl->authorizations[1]; |
| 92 | + $this->assertSame([Authorization::AGENT_CLASS_PUBLIC], $public->agentClasses); |
| 93 | + $this->assertSame([Authorization::MODE_READ], $public->modes); |
| 94 | + $this->assertFalse($public->isDefault); |
| 95 | + } |
| 96 | + |
| 97 | + public function testGetAcl(): void |
| 98 | + { |
| 99 | + $aclTurtle = <<<'TURTLE' |
| 100 | +@prefix acl: <http://www.w3.org/ns/auth/acl#>. |
| 101 | +@prefix foaf: <http://xmlns.com/foaf/0.1/>. |
| 102 | +
|
| 103 | +<#owner> |
| 104 | + a acl:Authorization; |
| 105 | + acl:agent <http://example.com/user#me>; |
| 106 | + acl:mode acl:Read, acl:Write, acl:Control; |
| 107 | + acl:accessTo <http://pod.example/data/file.ttl>. |
| 108 | +TURTLE; |
| 109 | + |
| 110 | + $httpClient = new MockHttpClient([ |
| 111 | + // HEAD response with Link: rel="acl" |
| 112 | + new MockResponse('', [ |
| 113 | + 'http_code' => 200, |
| 114 | + 'response_headers' => [ |
| 115 | + 'Content-Type' => 'text/turtle', |
| 116 | + 'Link' => '<http://pod.example/data/file.ttl.acl>; rel="acl"', |
| 117 | + ], |
| 118 | + ]), |
| 119 | + // GET ACL document |
| 120 | + new MockResponse($aclTurtle, [ |
| 121 | + 'http_code' => 200, |
| 122 | + 'response_headers' => ['Content-Type' => 'text/turtle'], |
| 123 | + ]), |
| 124 | + ]); |
| 125 | + $client = new SolidClient($httpClient); |
| 126 | + |
| 127 | + $acl = $client->getAcl('http://pod.example/data/file.ttl'); |
| 128 | + |
| 129 | + $this->assertSame('http://pod.example/data/file.ttl.acl', $acl->aclUrl); |
| 130 | + $this->assertCount(1, $acl->authorizations); |
| 131 | + $this->assertSame(['http://example.com/user#me'], $acl->authorizations[0]->agents); |
| 132 | + } |
| 133 | + |
| 134 | + public function testPutAcl(): void |
| 135 | + { |
| 136 | + $response = new MockResponse('', ['http_code' => 201]); |
| 137 | + $httpClient = new MockHttpClient($response); |
| 138 | + $client = new SolidClient($httpClient); |
| 139 | + |
| 140 | + $acl = new AclDocument('http://pod.example/data/file.ttl.acl', [ |
| 141 | + new Authorization( |
| 142 | + 'http://pod.example/data/file.ttl', |
| 143 | + ['http://example.com/user#me'], |
| 144 | + [], |
| 145 | + [Authorization::MODE_READ, Authorization::MODE_WRITE], |
| 146 | + ), |
| 147 | + ]); |
| 148 | + |
| 149 | + $client->putAcl('http://pod.example/data/file.ttl', $acl); |
| 150 | + |
| 151 | + $this->assertSame('PUT', $response->getRequestMethod()); |
| 152 | + $this->assertSame('http://pod.example/data/file.ttl.acl', $response->getRequestUrl()); |
| 153 | + $this->assertStringContainsString('acl:agent', $response->getRequestOptions()['body']); |
| 154 | + } |
| 155 | + |
| 156 | + public function testAclDocumentWithDefault(): void |
| 157 | + { |
| 158 | + $acl = new AclDocument('http://pod.example/container/.acl', [ |
| 159 | + new Authorization( |
| 160 | + 'http://pod.example/container/', |
| 161 | + ['http://example.com/user#me'], |
| 162 | + [], |
| 163 | + [Authorization::MODE_READ, Authorization::MODE_WRITE], |
| 164 | + true, |
| 165 | + ), |
| 166 | + ]); |
| 167 | + |
| 168 | + $turtle = $acl->toTurtle(); |
| 169 | + |
| 170 | + $this->assertStringContainsString('acl:default', $turtle); |
| 171 | + } |
| 172 | +} |
0 commit comments