userList = [ 'u1' => Helper::makeUser('u1.' . randomString(6), 'collage', clearInbox: true), 'u2' => Helper::makeUser('u2.' . randomString(6), 'collage', clearInbox: true), 'u3' => Helper::makeUser('u3.' . randomString(6), 'collage', clearInbox: true), ]; $this->userList['u1']->requestContext()->setViewer($this->userList['u1']); $this->artistName = [ 'The phpunit ' . randomString(8) . ' Band', 'The phpunit ' . randomString(8) . ' Sisters', 'The phpunit ' . randomString(8) . ' Brothers', 'The phpunit ' . randomString(8) . ' Mothers', 'The phpunit ' . randomString(8) . ' Fathers', ]; $this->tgroupList = []; $artistMan = new Manager\Artist(); $user = $this->userList['u1']; $this->tgroupList = [ Helper::makeTGroupMusic( $user, 'Some ' . randomString(8) . ' songs', [[ARTIST_MAIN], [$this->artistName[0]]], $this->tagList(1), ), Helper::makeTGroupMusic( $user, 'Some ' . randomString(8) . ' songs', [[ARTIST_MAIN], [$this->artistName[1]]], $this->tagList(2), ), Helper::makeTGroupMusic( $user, 'Some ' . randomString(8) . ' songs', [[ARTIST_MAIN], [$this->artistName[2], $this->artistName[3]]], $this->tagList(3), ), Helper::makeTGroupMusic( $user, 'Some ' . randomString(8) . ' songs', [[ARTIST_MAIN], [$this->artistName[3]]], $this->tagList(4), ), Helper::makeTGroupMusic( $user, 'Some ' . randomString(8) . ' songs', [[ARTIST_MAIN], [$this->artistName[4]]], $this->tagList(5), ), ]; } public function tearDown(): void { if (isset($this->collageList)) { foreach ($this->collageList as $collage) { $collage->toggleAttr('sort-newest', false); $collage->hardRemove(); } } foreach ($this->tgroupList as $tgroup) { $tgroup->remove($this->userList['u1']); } foreach ($this->userList as $user) { $user->remove(); } } public function testArgParse(): void { $this->collageList[] = (new Manager\Collage())->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage lock ' . randomString(20), description: 'phpunit collage lock description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; // NB: These objects should never be instantiated directly // We only do so here in order to test them. $inner = new Collage\TGroup($collage); $this->assertEquals( [], $inner->parseUrlArgs('', 'li[]'), 'collage-parse-no-arg' ); $this->assertEquals( [], $inner->parseUrlArgs('y=1&y=2&y=3', 'x'), 'collage-parse-no-match' ); $this->assertEquals( [33, 44], $inner->parseUrlArgs('a=11&b=33&a=22&b=44', 'b'), 'collage-parse-some' ); $this->assertEquals( [14, 31, 58, 68, 69, 54, 5], $inner->parseUrlArgs( 'li[]=14&li[]=31&li[]=58&li[]=68&li[]=69&li[]=54&li[]=5', 'li[]' ), 'collage-parse-args' ); } public function test00CollageCreate(): void { $manager = new Manager\Collage(); $stats = new Stats\Collage(); $total = $stats->collageTotal(); $name = 'phpunit collage create ' . randomString(20); $description = 'phpunit collage create description'; $tagList = $this->tagList(3); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: $name, description: $description, tagList: implode(' ', $tagList), ); $collage = $this->collageList[0]; $this->assertEquals($total + 1, $stats->collageTotal(), 'collage-stats-total'); $this->assertEquals($total + 2, $stats->increment(), 'collage-stats-increment'); $this->assertEquals($collage->id, $manager->findById($collage->id)?->id, 'collage-find-by-id'); $this->assertEquals(0, $collage->maxGroups(), 'collage-max-group'); $this->assertEquals(0, $collage->maxGroupsPerUser(), 'collage-max-per-user'); $this->assertEquals(0, $collage->numEntries(), 'collage-num-subcribers'); $this->assertEquals(1, $collage->categoryId(), 'collage-id'); $this->assertEquals($description, $collage->description(), 'collage-description'); $this->assertEquals($name, $collage->name(), 'collage-name'); $this->assertEquals($this->userList['u1']->id, $collage->ownerId(), 'collage-owner-id'); $this->assertEquals($tagList, $collage->tags(), 'collage-tag-list'); $this->assertFalse($collage->sortNewest(), 'collage-sort-initial'); $this->assertFalse($collage->isArtist(), 'collage-is-not-artist'); $this->assertFalse($collage->isDeleted(), 'collage-is-not-deleted'); $this->assertFalse($collage->isFeatured(), 'collage-is-not-featured'); $this->assertFalse($collage->isOwner($this->userList['u2']), 'collage-is-not-owner'); $this->assertTrue($collage->isOwner($this->userList['u1']), 'collage-is-owner'); $this->assertStringContainsString($collage->name(), $collage->link(), 'collage-link'); $this->assertFalse($collage->hasAttr('sort-newest'), 'collage-no-sort-newest'); $this->assertEquals( 1, $this->userList['u1']->stats()->collageTotal(), 'collage-user-total' ); $find = $manager->findByName($name); $this->assertEquals($collage->id, $find?->id, 'collage-find-by-name'); $this->userList['u1']->addCustomPrivilege('site_collages_manage'); } public function testCollageAdd(): void { $manager = new Manager\Collage(); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage add ' . randomString(20), description: 'phpunit collage add description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; $this->assertEquals(0, $collage->numSubscribers(), 'collage-num-subcribers'); $this->assertFalse($collage->isSubscribed($this->userList['u2']), 'collage-is-not-subcribed'); $this->assertEquals(1, $collage->toggleSubscription($this->userList['u2']), 'collage-subcribe'); $this->assertTrue($collage->isSubscribed($this->userList['u2']), 'collage-is-subcribed'); $this->assertEquals(1, $collage->numSubscribers(), 'collage-one-subcriber'); $this->assertFalse($collage->isPersonal(), 'collage-is-not-personal'); $this->assertFalse($collage->userCanContribute($this->userList['u3']), 'collage-cannot-contribute'); $this->assertTrue($this->userList['u3']->addCustomPrivilege('site_collages_manage'), 'user-can-collaborate'); $this->assertTrue($this->userList['u3']->permitted('site_collages_manage'), 'user-can-manage-collage'); $this->assertTrue($collage->userCanContribute($this->userList['u3']), 'collage-can-contribute'); // add an entry // If an entry is added in the same second as a user subscribes, the condition is false. // A subscriber is considered to have a new entry in a collage when: // collages_torrents.AddedOn > users_collage_subs.LastVisit // This is why we must Helper::sleepTick(); $this->assertEquals(1, $collage->addEntry($this->tgroupList[0], $this->userList['u3'], 'collage-add-entry')); $this->assertEquals(0, $collage->addEntry($this->tgroupList[0], $this->userList['u2'], 'collage-add-dupe-entry')); $unread = $manager->subscribedTGroupCollageList($this->userList['u2'], false); $this->assertCount(1, $unread, 'collage-one-unread'); // catchup // TODO: this relies on a side effect in isSubscribed(), the clearing should be more explicit $this->assertTrue($collage->isSubscribed($this->userList['u2']), 'collage-catchup'); $this->assertCount( 0, $manager->subscribedTGroupCollageList($this->userList['u2'], false), 'collage-none-unread' ); $this->assertEquals(1, $collage->toggleSubscription($this->userList['u2']), 'collage-unsubscribe'); $this->assertFalse($collage->isSubscribed($this->userList['u2']), 'collage-is-no-longer-subcribed'); $this->assertEquals(0, $collage->numSubscribers(), 'collage-no-subcribers'); $this->assertStringStartsWith(date('Y-m-d '), $collage->entryCreated($this->tgroupList[0]), 'collage-entry-created'); } public function testCollageArtist(): void { $manager = new Manager\Collage(); $this->collageList = [ $manager->create( user: $this->userList['u1'], categoryId: CollageType::artist->value, name: 'phpunit collage artist ' . randomString(20), description: 'phpunit collage artist description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u1'], categoryId: CollageType::artist->value, name: 'phpunit collage artist ' . randomString(20), description: 'phpunit collage artist description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u1'], categoryId: CollageType::artist->value, name: 'phpunit collage artist ' . randomString(20), description: 'phpunit collage artist description', tagList: implode(' ', $this->tagList(4)), ), ]; $artistMan = new Manager\Artist(); $artistList = []; foreach ($this->artistName as $artistName) { $artist = $artistMan->findByName($artistName); $this->assertNotNull($artist, "find $artistName"); $this->collageList[0]->addEntry($artistMan->findByName($artistName), $this->userList['u1']); $artistList[] = $artist; } $this->collageList[1]->addEntry($artistList[0], $this->userList['u1']); $this->assertCount(1, $manager->addToArtistCollageDefault($artistList[0], $this->userList['u1']), 'collage-default-add-acollage-never'); $this->collageList[1]->addEntry($artistList[1], $this->userList['u1']); $default = $manager->addToArtistCollageDefault($artistList[1], $this->userList['u1']); $this->assertCount(1, $default, 'collage-default-add-acollage-added'); $this->assertEquals($this->collageList[2]->id, $default[0]->id, 'collage-default-artist-suggestion'); $this->assertEquals(1, $this->collageList[1]->toggleSubscription($this->userList['u1']), 'collage-artist-subscribe'); $this->collageList[1]->addEntry($artistList[2], $this->userList['u2']); $this->collageList[1]->addEntry($artistList[3], $this->userList['u3']); $this->assertEquals(3, $this->collageList[1]->numContributors(), 'collage-artist-contributor'); $artist = $artistMan->findByName($this->artistName[2]); $this->assertInstanceOf(Artist::class, $artist, 'collage-artist-found'); $summary = $manager->artistSummary($artist); $this->assertEquals(2, $summary['total'], 'collage-artist-summary-total'); $this->assertCount(2, $summary['above'], 'collage-artist-summary-above'); $this->assertCount(0, $summary['below'], 'collage-artist-summary-below'); $this->assertTrue($this->collageList[1]->isSubscribed($this->userList['u1']), 'collage-artist-catchup'); $this->assertCount( 0, $manager->subscribedArtistCollageList($this->userList['u1'], false), 'collage-artist-unread' ); $this->assertStringStartsWith( date('Y-m-d '), $this->collageList[1]->entryCreated($artistMan->findByName($this->artistName[3])), 'collage-artist-entry-created' ); $this->assertInstanceOf( Manager\Collage::class, $manager->flushDefaultArtist($this->userList['u1']), 'collage-flush-default-artist-suggestion' ); } public function testCollageAPI(): void { $manager = new Manager\Collage(); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::label->value, name: 'phpunit collage api ' . randomString(20), description: 'phpunit collage api description', tagList: implode(' ', $this->tagList(3)), ); $_GET['req'] = 'collage'; $_GET['collage_id'] = $this->collageList[0]->id; $this->assertEquals( [ 'ID' => $this->collageList[0]->id(), 'Name' => $this->collageList[0]->name(), 'CategoryID' => $this->collageList[0]->categoryId(), 'Category' => null, ], new API\Collage([])->run(), 'collage-api', ); } public function testCollageContribute(): void { $manager = new Manager\Collage(); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::label->value, name: 'phpunit collage contrib ' . randomString(20), description: 'phpunit collage contrib description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; $this->assertEquals(0, $collage->numContributors(), 'collage-no-contributors'); // contribute! contribute! $u1 = $this->userList['u1']; $u2 = $this->userList['u2']; $u3 = $this->userList['u3']; $this->assertCount(0, $manager->addToCollageDefault($this->tgroupList[1], $u2), 'collage-default-add-tcollage-never'); $collage->addEntry($this->tgroupList[0], $u1); $default = $manager->addToCollageDefault($this->tgroupList[1], $u1); $this->assertCount(1, $default, 'collage-default-add-tcollage-added'); $this->assertEquals($collage->id, $default[0]->id, 'collage-default-suggestion'); $this->assertInstanceOf( Manager\Collage::class, $manager->flushDefaultGroup($u1), 'collage-flush-default-suggestion' ); $collage->addEntry($this->tgroupList[1], $u3); $collage->addEntry($this->tgroupList[2], $u3); $collage->addEntry($this->tgroupList[3], $u3); $this->assertEquals(4, $collage->numEntries(), 'collage-has-4'); $this->assertCount(4, $collage->topArtists(), 'collage-has-4-artists'); $this->assertEquals(2, $collage->numContributors(), 'collage-two-contributors'); $this->assertEquals( [ $this->userList['u1']->id => 1, $this->userList['u3']->id => 3, ], $collage->contributors(), 'collage-contributor-list' ); $this->assertTrue($collage->userHasContributed($u1), 'collage-user-1-has-contrib'); $this->assertFalse($collage->userHasContributed($u2), 'collage-user-2-no-contrib'); $this->assertTrue($collage->userHasContributed($u3), 'collage-user-3-has-contrib'); $this->assertEquals(3, $collage->contributionTotal($u3), 'collage-contributor-three'); $this->assertEquals($u1->id, $collage->entryUserId($this->tgroupList[0]), 'collage-contribution-by'); $idList = array_map(fn($n) => $this->tgroupList[$n]->id, range(0, 3)); $this->assertEquals([$idList[0], $idList[1], $idList[2], $idList[3]], $collage->entryList(), 'collage-entry-list'); $this->assertEquals(1, $collage->updateSequenceEntry($this->tgroupList[2], 1000), 'collage-entry-to-last'); $this->assertEquals([$idList[0], $idList[1], $idList[3], $idList[2]], $collage->entryList(), 'collage-new-entry-last'); $newOrder = [$idList[1], $idList[3], $idList[0], $idList[2]]; $collage->updateSequence(implode('&', array_map(fn($id) => "li[]=$id", $newOrder))); $this->assertEquals($newOrder, $collage->entryList(), 'collage-new-entry-list'); $entry = 2; $this->assertEquals($entry * 10, $collage->sequence($newOrder[$entry - 1]), 'collage-entry-sequence'); $this->assertInstanceOf( Manager\Collage::class, $manager->setImageProxy(new Util\ImageProxy($u1)), 'collage-manager-image-proxy' ); $cover = $manager->tgroupCover($this->tgroupList[0]); $this->assertStringContainsString("image_group_{$this->tgroupList[0]->id}", $cover, 'collage-tgroup-cover-id'); $this->assertStringContainsString($this->tgroupList[0]->name(), $cover, 'collage-tgroup-cover-name'); $this->assertEquals(1, $collage->removeEntry($this->tgroupList[1]), 'collage-remove-entry'); $this->assertEquals(3, $collage->numEntries(), 'collage-has-3'); $this->assertEquals([$idList[3], $idList[0], $idList[2]], $collage->entryList(), 'collage-removed-entry-list'); $this->assertTrue($collage->toggleAttr('sort-newest', true), 'collage-personal-sort-newest'); $collage->addEntry($this->tgroupList[1], $u3); $this->assertEquals([$idList[1], $idList[3], $idList[0], $idList[2]], $collage->entryList(), 'collage-add-first'); } public function testCollageFeature(): void { $manager = new Manager\Collage(); $user = $this->userList['u1']; $this->collageList = [ $manager->create( user: $user, categoryId: CollageType::personal->value, name: 'phpunit collage feat 001 ' . randomString(20), description: 'phpunit collage feature description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $user, categoryId: CollageType::personal->value, name: 'phpunit collage feat 002 ' . randomString(20), description: 'phpunit collage feature description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $user, categoryId: CollageType::personal->value, name: 'phpunit collage feat 003 ' . randomString(20), description: 'phpunit collage feature description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $user, categoryId: CollageType::personal->value, name: 'phpunit collage feat 004 ' . randomString(20), description: 'phpunit collage feature description', tagList: implode(' ', $this->tagList(3)), ), ]; $personal = $manager->findPersonalByUser($user); $this->assertEquals( array_map(fn($c) => $c->id, [$this->collageList[0], $this->collageList[1], $this->collageList[2], $this->collageList[3]]), array_map(fn($c) => $c->id, $personal), 'collage-personal-list' ); $this->assertTrue($personal[2]->setFeatured()->modify(), 'collage-set-featured'); $personal = $manager->findPersonalByUser($user); $this->assertEquals( array_map(fn($c) => $c->id, [$this->collageList[2], $this->collageList[0], $this->collageList[1], $this->collageList[3]]), array_map(fn($c) => $c->id, $personal), 'collage-personal-list-featured' ); foreach ($this->collageList as $collage) { $collage->addEntry($this->tgroupList[0], $user); } $summary = $manager->tgroupPersonalSummary($this->tgroupList[0]); $this->assertEquals(4, $summary['total'], 'collage-manager-personal-tgroup-total'); $this->assertCount(COLLAGE_SAMPLE_THRESHOLD, $summary['above'], 'collage-manager-personal-tgroup-above'); $this->assertEquals(COLLAGE_SAMPLE_THRESHOLD, count($this->collageList) - count($summary['below']), 'collage-manager-personal-tgroup-below'); } public function testCollageJson(): void { $this->collageList[] = (new Manager\Collage())->create( user: $this->userList['u1'], categoryId: CollageType::staffPick->value, name: 'phpunit collage json ' . randomString(20), description: 'phpunit collage json description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; foreach (range(0, 3) as $n) { $collage->addEntry($this->tgroupList[$n], $this->userList['u3']); } $this->assertTrue((new User\Bookmark($this->userList['u1']))->create('collage', $collage->id), 'collage-bookmark'); $payload = (new Json\Collage( $collage, 1, $this->userList['u1'], new Manager\TGroup(), new Manager\Torrent(), ))->payload(); $this->assertEquals($collage->id, $payload['id'], 'collage-json-id'); $this->assertEquals('Staff picks', $payload['collageCategoryName'], 'collage-json-cat-name'); $this->assertCount(4, $payload['torrentGroupIDList'], 'collage-json-entry-count'); $this->assertTrue($payload['hasBookmarked'], 'collage-json-bookmarked'); } public function testCollageLock(): void { $this->collageList[] = (new Manager\Collage())->create( user: $this->userList['u1'], categoryId: CollageType::chart->value, name: 'phpunit collage lock ' . randomString(20), description: 'phpunit collage lock description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; $this->assertFalse($collage->isLocked(), 'collage-is-not-locked'); $this->assertTrue($collage->toggleLocked()->modify(), 'collage-lock'); $this->assertTrue($collage->isLocked(), 'collage-is-now-locked'); $this->assertFalse($collage->userCanContribute($this->userList['u1']), 'collage-locked-contribute'); $this->assertTrue($collage->toggleLocked()->modify(), 'collage-unlock'); $this->assertFalse($collage->isLocked(), 'collage-is-unlocked'); } public function testCollageManager(): void { $manager = new Manager\Collage(); $this->assertEquals( $this->userList['u1']->username() . "'s personal collage", $manager->personalCollageName($this->userList['u1']->username()), 'collage-manager-personal-name' ); $stem = 'phpunit collage man '; $this->collageList = [ $manager->create( user: $this->userList['u1'], categoryId: CollageType::series->value, name: $stem . randomString(20), description: 'phpunit collage man description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u1'], categoryId: CollageType::series->value, name: $stem . randomString(20), description: 'phpunit collage man description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u1'], categoryId: CollageType::series->value, name: $stem . randomString(20), description: 'phpunit collage man description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u2'], categoryId: CollageType::series->value, name: $stem . randomString(20), description: 'phpunit collage man description', tagList: implode(' ', $this->tagList(3)), ), $manager->create( user: $this->userList['u2'], categoryId: CollageType::genre->value, name: $stem . randomString(20), description: 'phpunit collage man description', tagList: implode(' ', $this->tagList(3)), ), ]; $this->assertCount( count($this->collageList), $manager->autocomplete($stem), 'collage-manager-autocomplete' ); $nameList = array_map(fn($c) => $c->name(), $this->collageList); usort($nameList, 'strcasecmp'); $autocomplete = $manager->autocomplete('phpunit collage auto'); $this->assertEquals($nameList, array_column($autocomplete, 'value'), 'collage-manager-autocomplete'); foreach ($this->collageList as $collage) { $collage->addEntry($this->tgroupList[0], $this->userList['u3']); } $summary = $manager->tgroupGeneralSummary($this->tgroupList[0]); $this->assertEquals(5, $summary['total'], 'collage-manager-summary-tgroup-total'); $this->assertCount(COLLAGE_SAMPLE_THRESHOLD, $summary['above'], 'collage-manager-summary-tgroup-above'); $this->assertEquals(COLLAGE_SAMPLE_THRESHOLD, count($this->collageList) - count($summary['below']), 'collage-manager-summary-tgroup-below'); } public function testCollagePersonal(): void { $this->collageList[] = (new Manager\Collage())->create( user: $this->userList['u1'], categoryId: CollageType::personal->value, name: 'phpunit collage personal ' . randomString(20), description: 'phpunit collage personal description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; // $this->userList['u1']->addCustomPrivilege('site_collages_manage'); // $this->userList['u2']->addCustomPrivilege('site_collages_manage'); $this->assertTrue($collage->isPersonal(), 'collage-is-not-personal'); $this->assertTrue($collage->userCanContribute($this->userList['u1']), 'collage-can-contribute-personal'); $this->assertFalse($collage->userCanContribute($this->userList['u2']), 'collage-cannot-contribute-personal'); $collage->addEntry($this->tgroupList[0], $this->userList['u1']); $collage->addEntry($this->tgroupList[1], $this->userList['u3']); $collage->addEntry($this->tgroupList[3], $this->userList['u3']); $this->assertEquals( [ $this->tgroupList[3]->id, $this->tgroupList[1]->id, $this->tgroupList[0]->id, ], $collage->entryList(), 'collage-personal-newest-first' ); $this->assertTrue($collage->hasEntry($this->tgroupList[1]), 'collage-entry-present'); $this->assertFalse($collage->hasEntry($this->tgroupList[2]), 'collage-entry-absent'); $this->assertTrue($collage->toggleAttr('sort-newest', true), 'collage-personal-sort-newest'); $collage->addEntry($this->tgroupList[2], $this->userList['u2']); $this->assertEquals( [ $this->tgroupList[3]->id, $this->tgroupList[1]->id, $this->tgroupList[0]->id, $this->tgroupList[2]->id, ], $collage->entryList(), 'collage-personal-newest-last' ); $this->assertTrue($collage->toggleAttr('sort-newest', false), 'collage-personal-toggle-newest'); $collage->addEntry($this->tgroupList[4], $this->userList['u2']); $this->assertEquals( [ $this->tgroupList[4]->id, $this->tgroupList[3]->id, $this->tgroupList[1]->id, $this->tgroupList[0]->id, $this->tgroupList[2]->id, ], $collage->entryList(), 'collage-personal-newest-not-last' ); // given that the default tag lists are successively longer slices // of the same list, the frequency is the same order as the source // list. $this->assertEquals( ["acoustic", "jazz", "metal", "rock"], $collage->rebuildTagList(), 'collage-personal-rebuild-taglist' ); } public function testCollageRemove(): void { $manager = new Manager\Collage(); $name = 'phpunit collage remove ' . randomString(20); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: $name, description: 'phpunit collage remove description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; $this->assertEquals(1, $collage->remove(), 'collage-remove'); $this->assertTrue($collage->isDeleted(), 'collage-is-deleted'); $this->assertNull($manager->recoverByName($name . 'does not exist'), 'collage-recover-fail'); $this->assertInstanceOf(Collage::class, $manager->recoverByName($name), 'collage-recover-by-name'); $collage->remove(); $this->assertInstanceOf(Collage::class, $manager->recoverById($collage->id), 'collage-recover-by-id'); } public function testCollageAjaxAdd(): void { $name = 'phpunit collage ajax ' . randomString(20); $manager = new Manager\Collage(); $collage = $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::personal->value, name: $name, description: 'phpunit collage ajax description', tagList: implode(' ', $this->tagList(3)), ); $byEntry = new Json\Ajax\CollageAdd( collage: $collage, entry: $this->tgroupList[0], user: $this->userList['u1'], ); $this->assertArrayHasKey('link', $byEntry->payload(), 'collage-ajax-add-entry-id'); $fail = new Json\Ajax\CollageAdd( collage: $collage, entry: $this->tgroupList[0], user: $this->userList['u1'], ); $this->assertEquals([], $fail->payload(), 'collage-ajax-add-already'); $response = json_decode($fail->response(), true); $this->assertEquals('already present?', $response['error'], 'collage-ajax-error-already'); $fail = new Json\Ajax\CollageAdd( collage: $collage, entry: $this->tgroupList[1], user: $this->userList['u2'], ); $response = json_decode($fail->response(), true); $this->assertEquals('personal', $response['error'], 'collage-ajax-error-personal'); $collage->toggleLocked()->modify(); $fail = new Json\Ajax\CollageAdd( collage: $collage, entry: $this->tgroupList[1], user: $this->userList['u1'], ); $response = json_decode($fail->response(), true); $this->assertEquals('locked', $response['error'], 'collage-ajax-error-locked'); } public function testCollageType(): void { $manager = new Manager\Collage(); $this->assertEquals(CollageType::personal, $manager->findType(CollageType::personal->value), 'collage-type-personal'); $this->assertEquals(CollageType::artist, $manager->findType(CollageType::artist->value), 'collage-type-artist'); $this->assertEquals(CollageType::genre, $manager->findType(2), 'collage-type-genre'); } public function testCollageShuffle(): void { $manager = new Manager\Collage(); $list = [10, 20, 30, 40]; $shuffle = $manager->listShuffle(3, $list); $this->assertEquals(4, $shuffle['total'], 'shuffle-full-total'); $this->assertCount(3, $shuffle['above'], 'shuffle-full-above'); $this->assertCount(1, $shuffle['below'], 'shuffle-full-below'); $shuffle = $manager->listShuffle(5, $list); $this->assertEquals(count($list), count($shuffle['above']), 'shuffle-partial-above'); $this->assertCount(0, $shuffle['below'], 'shuffle-partial-below'); } public function testCollageNotification(): void { $manager = new Manager\Collage(); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage notif ' . randomString(20), description: 'phpunit collage notif description', tagList: implode(' ', $this->tagList(3)), ); $subscriber = $this->userList['u2']; $adder = $this->userList['u3']; $collage = $this->collageList[0]; $collage->toggleSubscription($subscriber); $collage->addEntry($this->tgroupList[0], $adder); $list = $manager->subscribedTGroupCollageList($subscriber, true); $this->assertCount(1, $list, 'collage-user-sub-list-total'); $entry = current($list); $this->assertEquals( $collage->id, $entry['collageId'], 'collage-user-entry-collageid', ); $this->assertEquals( $collage->numEntries(), $entry['nrEntries'], 'collage-user-entry-nr-entry', ); $this->assertTrue( Helper::recentDate($entry['lastVisit']), 'collage-user-entry-last-visit', ); $this->assertEquals( [$this->tgroupList[0]->id], $entry['groupIds'], 'collage-user-group-id', ); $this->assertEquals( $this->tgroupList[0]->id, $entry['tgroup_list'][0]->id, 'collage-user-tgroup-list-id', ); $collage->addEntry($this->tgroupList[1], $adder); Helper::sleepTick(); $notifier = new User\Notification\Collage($subscriber); $this->assertEquals( 1, $notifier->clearCollage($collage), 'collage-user-sub-clear-collage' ); $collage->addEntry($this->tgroupList[2], $adder); Helper::sleepTick(); $this->assertEquals( 1, $notifier->clear(), 'collage-user-sub-clear-all' ); } }