Failed Conditions
Push — master ( b7bcff...572aee )
by
unknown
03:12
created

CardTest::testValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 25
rs 9.6333
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Model;
6
7
use Application\DBAL\Types\SiteType;
8
use Application\Model\Card;
9
use Application\Model\Change;
10
use Application\Model\Collection;
11
use Application\Model\Country;
12
use Application\Model\Tag;
13
use Application\Model\User;
14
use Cake\Chronos\Chronos;
0 ignored issues
show
Bug introduced by
The type Cake\Chronos\Chronos was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use GraphQL\Doctrine\Definition\EntityID;
16
use PHPUnit\Framework\TestCase;
17
18
class CardTest extends TestCase
19
{
20
    protected function tearDown(): void
21
    {
22
        User::setCurrent(null);
23
        Chronos::setTestNow(null);
24
    }
25
26
    public function testName(): void
27
    {
28
        $withoutName = new Card();
29
        self::assertSame('', $withoutName->getName());
30
31
        $withName = new Card('test name');
32
        self::assertSame('test name', $withName->getName());
33
    }
34
35
    public function testGetPath(): void
36
    {
37
        $card = new Card();
38
        $card->setFilename('photo.jpg');
39
40
        self::assertSame('photo.jpg', $card->getFilename());
41
        $appPath = realpath('.');
42
        $expected = $appPath . '/data/images/photo.jpg';
43
        self::assertSame($expected, $card->getPath());
44
    }
45
46
    public function testSetDating(): void
47
    {
48
        $card = new Card();
49
50
        self::assertCount(0, $card->getDatings());
51
52
        $card->setDating('2000');
53
        self::assertCount(1, $card->getDatings());
54
        $original = $card->getDatings()->first();
55
56
        $card->setDating('2000');
57
        self::assertCount(1, $card->getDatings());
58
        self::assertSame($original, $card->getDatings()->first(), 'must be same dating');
59
60
        $card->setDating('1980-1990');
61
        self::assertCount(1, $card->getDatings());
62
        self::assertNotSame($original, $card->getDatings()->first(), 'must be new one');
63
64
        $card->setDating('');
65
        self::assertCount(0, $card->getDatings());
66
    }
67
68
    public function testCopyInto(): void
69
    {
70
        $admin = new User(User::ROLE_ADMINISTRATOR);
71
        User::setCurrent($admin);
72
73
        $suggestion = new Card();
74
        $suggestion->setSite(SiteType::DILPS);
75
        $suggestion->setVisibility(Card::VISIBILITY_MEMBER);
76
        $suggestion->setCode('code-suggestion');
77
        $suggestion->setName('test name');
78
        $suggestion->setDating('2010');
79
        $suggestion->setArtists(['John', 'Sarah']);
80
        $suggestion->setInstitution('Museum');
81
        $suggestion->setCountry(new Country());
82
        $suggestion->timestampCreation();
83
        $suggestion->setWidth(123);
84
        $suggestion->setHeight(123);
85
        $suggestion->setFileSize(123);
86
        $suggestion->setFilename('foo.png');
87
        touch($suggestion->getPath());
88
89
        $collection = new Collection();
90
        $suggestion->addCollection($collection);
91
92
        $original = new Card();
93
        $original->setVisibility(Card::VISIBILITY_PUBLIC);
94
        $original->setCode('code-original');
95
        $original->setWidth(456);
96
        $original->setHeight(456);
97
        $original->setFileSize(456);
98
        $suggestion->setOriginal($original);
99
        $suggestion->copyInto($original);
100
101
        self::assertSame(Card::VISIBILITY_PUBLIC, $original->getVisibility());
102
        self::assertSame('code-original', $original->getCode());
103
        self::assertSame('test name', $original->getName());
104
        self::assertSame('2010', $original->getDating());
105
        self::assertSame('2010-01-01T00:00:00+00:00', $original->getDatings()->first()->getFrom()->format('c'), 'datings should be re-computed');
106
        self::assertCount(2, $original->getArtists(), 'artists should be copied');
107
        self::assertSame('Museum', $original->getInstitution()->getName(), 'institution should be copied');
108
        self::assertNotNull($original->getCountry(), 'country should be copied');
109
        self::assertNull($original->getOriginal(), 'original should not be copied over');
110
        self::assertCount(0, $original->getCollections(), 'original should not be moved to intro a collection');
111
112
        self::assertNotEquals('', $original->getFilename(), 'should have file on disk');
113
        self::assertNotEquals($suggestion->getFilename(), $original->getFilename(), 'should not share the same file on disk');
114
        self::assertFileExists($original->getPath(), 'file on disk should have been copied');
115
116
        self::assertSame(123, $original->getWidth());
117
        self::assertSame(123, $original->getHeight());
118
        self::assertSame(123, $original->getFileSize());
119
    }
120
121
    public function testCopyIntoWithoutFile(): void
122
    {
123
        $original = new Card();
124
        $original->setWidth(456);
125
        $original->setHeight(456);
126
        $original->setFileSize(456);
127
128
        $suggestion = new Card();
129
        $suggestion->copyInto($original);
130
131
        self::assertSame(456, $original->getWidth());
132
        self::assertSame(456, $original->getHeight());
133
        self::assertSame(456, $original->getFileSize());
134
    }
135
136
    public function testRelatedCards(): void
137
    {
138
        $card1 = new Card();
139
        $card2 = new Card();
140
141
        self::assertCount(0, $card1->getCards());
142
        self::assertCount(0, $card2->getCards());
143
144
        $card1->addCard($card2);
145
146
        self::assertCount(1, $card1->getCards());
147
        self::assertCount(1, $card2->getCards());
148
149
        self::assertSame($card2, $card1->getCards()->first());
150
        self::assertSame($card1, $card2->getCards()->first());
151
152
        $card2->removeCard($card1);
153
154
        self::assertCount(0, $card1->getCards());
155
        self::assertCount(0, $card2->getCards());
156
    }
157
158
    public function testRelatedCardWithSelf(): void
159
    {
160
        $this->expectExceptionMessage('A card cannot be related to itself');
161
        $card = new Card();
162
        $card->addCard($card);
163
    }
164
165
    public function testGetPermissions(): void
166
    {
167
        $card = new Card();
168
        $card->setSite(SiteType::DILPS);
169
        $actual = $card->getPermissions();
170
        $expected = [
171
            'create' => false,
172
            'read' => true,
173
            'update' => false,
174
            'delete' => false,
175
        ];
176
        self::assertEquals($expected, $actual, 'should be able to get permissions as anonymous');
177
178
        // Make it the current user as creator
179
        $user = new User();
180
        $user->setSite(SiteType::DILPS);
181
        User::setCurrent($user);
182
        $card->timestampCreation();
183
184
        $actual2 = $card->getPermissions();
185
        $expected2 = [
186
            'create' => true,
187
            'read' => true,
188
            'update' => false,
189
            'delete' => false,
190
        ];
191
        self::assertEquals($expected2, $actual2, 'should be able to get permissions as creator');
192
    }
193
194
    public function testChange(): void
195
    {
196
        $card = new Card();
197
        $change = new Change();
198
        self::assertNull($card->getChange());
199
200
        $change->setSuggestion($card);
201
        self::assertSame($change, $card->getChange());
202
203
        $change->setSuggestion(null);
204
        self::assertNull($card->getChange());
205
    }
206
207
    /**
208
     * @dataProvider providerSetVisibility
209
     */
210
    public function testSetVisibility(string $role, string $previous, string $next, bool $shouldThrow): void
211
    {
212
        $admin = new User(User::ROLE_ADMINISTRATOR);
213
        User::setCurrent($admin);
214
        $card = new Card();
215
        $card->setVisibility($previous);
216
217
        $user = new User($role);
218
        User::setCurrent($user);
219
220
        if ($shouldThrow) {
221
            $this->expectExceptionMessage('Only administrator can make a card public');
222
        }
223
224
        $card->setVisibility($next);
225
        self::assertSame($next, $card->getVisibility());
226
    }
227
228
    public function providerSetVisibility(): iterable
229
    {
230
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_PRIVATE, false];
231
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_MEMBER, false];
232
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_PUBLIC, true];
233
        yield [User::ROLE_STUDENT, Card::VISIBILITY_MEMBER, Card::VISIBILITY_PRIVATE, false];
234
        yield [User::ROLE_STUDENT, Card::VISIBILITY_MEMBER, Card::VISIBILITY_MEMBER, false];
235
        yield [User::ROLE_STUDENT, Card::VISIBILITY_MEMBER, Card::VISIBILITY_PUBLIC, true];
236
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_PRIVATE, false];
237
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_MEMBER, false];
238
        yield [User::ROLE_STUDENT, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_PUBLIC, false];
239
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_PRIVATE, false];
240
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_MEMBER, false];
241
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PRIVATE, Card::VISIBILITY_PUBLIC, false];
242
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_MEMBER, Card::VISIBILITY_PRIVATE, false];
243
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_MEMBER, Card::VISIBILITY_MEMBER, false];
244
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_MEMBER, Card::VISIBILITY_PUBLIC, false];
245
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_PRIVATE, false];
246
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_MEMBER, false];
247
        yield [User::ROLE_ADMINISTRATOR, Card::VISIBILITY_PUBLIC, Card::VISIBILITY_PUBLIC, false];
248
    }
249
250
    public function testSetInstitution(): void
251
    {
252
        $card = new Card();
253
        $card->setSite(SiteType::DILPS);
254
        self::assertNull($card->getInstitution());
255
256
        $card->setInstitution('foo');
257
        $institution1 = $card->getInstitution();
258
        self::assertSame('foo', $institution1->getName(), 'can set new institution');
259
260
        $card->setInstitution('foo');
261
        $institution2 = $card->getInstitution();
262
        self::assertSame($institution1, $institution2, 'change for same same will have no effect at all');
263
        self::assertSame('foo', $institution2->getName(), 'did not change');
264
265
        $card->setInstitution('bar');
266
        $institution3 = $card->getInstitution();
267
        self::assertNotSame($institution1, $institution3, 'can change for something else');
268
        self::assertSame('bar', $institution3->getName(), 'new name');
269
    }
270
271
    public function testSetTags(): void
272
    {
273
        $card = new Card();
274
        $card->setSite('dilps');
275
276
        self::assertEquals([], $this->toIds($card->getTags()));
277
278
        $card->setTags([
279
            new EntityID(_em(), Tag::class, '4000'),
280
        ]);
281
        self::assertEquals([], $this->toIds($card->getTags()), 'still empty because not leaf');
282
283
        $card->setTags([
284
            new EntityID(_em(), Tag::class, '4001'),
285
        ]);
286
        self::assertEquals([4001, 4000], $this->toIds($card->getTags()), 'leaf added and parent automatically added');
287
288
        $card->getTags()->clear();
289
        self::assertEquals([], $this->toIds($card->getTags()));
290
291
        $card->setTags([
292
            new EntityID(_em(), Tag::class, '4000'),
293
            new EntityID(_em(), Tag::class, '4001'),
294
        ]);
295
        self::assertEquals([4001, 4000], $this->toIds($card->getTags()), 'also adding parent change nothing to result');
296
297
        $card->getTags()->clear();
298
        self::assertEquals([], $this->toIds($card->getTags()));
299
300
        $card->setTags([
301
            new EntityID(_em(), Tag::class, '4001'),
302
            new EntityID(_em(), Tag::class, '4004'),
303
        ]);
304
        self::assertEquals([4001, 4000], $this->toIds($card->getTags()), 'adding another parent change nothing to result');
305
306
        $card->setTags([
307
            new EntityID(_em(), Tag::class, '4001'),
308
            new EntityID(_em(), Tag::class, '4005'),
309
        ]);
310
        self::assertEquals([4001, 4000, 4005, 4004], $this->toIds($card->getTags()), 'adding two leaves select everything');
311
312
        $card->removeTag(_em()->getReference(Tag::class, 4000));
313
        self::assertEquals([4001, 4000, 4005, 4004], $this->toIds($card->getTags()), 'removing parent has no effect');
314
315
        $card->removeTag(_em()->getReference(Tag::class, 4001));
316
        self::assertEquals([4005, 4004], $this->toIds($card->getTags()), 'removing child remove hierarchy');
317
318
        $card->addTag(_em()->getReference(Tag::class, 4001));
319
        self::assertEquals([4005, 4004, 4001, 4000], $this->toIds($card->getTags()), 'adding again child re-add hierarchy');
320
    }
321
322
    private function toIds(iterable $models): array
323
    {
324
        $ids = [];
325
        foreach ($models as $model) {
326
            $ids[] = $model->getId();
327
        }
328
329
        return $ids;
330
    }
331
332
    public function testCollectionRelation(): void
333
    {
334
        $card = new Card();
335
        self::assertCount(0, $card->getCollections(), 'should have no collections');
336
337
        $collection = new Collection();
338
339
        $card->addCollection($collection);
340
        self::assertCount(1, $card->getCollections(), 'should have the added collection');
341
        self::assertSame($collection, $card->getCollections()->first(), 'should be able to retrieve added collection');
342
343
        $card->addCollection($collection);
344
        self::assertCount(1, $card->getCollections(), 'should still have the same unique collection');
345
346
        $collection2 = new Collection();
347
        $card->addCollection($collection2);
348
        self::assertCount(2, $card->getCollections(), 'should be able to add second collection');
349
350
        $card->removeCollection($collection);
351
        self::assertCount(1, $card->getCollections(), 'should be able to remove first collection');
352
        self::assertSame($collection2, $card->getCollections()->first(), 'should be have only second collection left');
353
    }
354
}
355