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 = [ 'phpunit collage ' . randomString(6), 'phpunit collage ' . randomString(6), 'phpunit collage ' . randomString(6), 'phpunit collage ' . randomString(6), 'phpunit collage ' . randomString(6), ]; $this->tgroupList = []; $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->requestContext()->unsetViewer(); $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->total(), '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()->flush()->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 testCollageSearch(): void { $mockSearch = $this->getMockBuilder(Search\Collage::class)->onlyMethods(['hiddenTags'])->getMock(); $mockSearch->method('hiddenTags')->willReturn(['test','spam']); $manager = new Manager\Collage(); $initial = sizeof($mockSearch->setLookup('name')->page(100, 0)); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage add ' . randomString(20), description: 'phpunit collage add description', tagList: 'jazz metal rap', ); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage add ' . randomString(20), description: 'phpunit collage add description', tagList: 'funk spam rock', ); $result = $mockSearch->page(100, 0); $this->assertCount($initial + 1, $result, 'collage-search-results'); } public function testCollageSearchUnfiltered(): void { $mockSearch = $this->getMockBuilder(Search\Collage::class)->onlyMethods(['hiddenTags'])->getMock(); $mockSearch->method('hiddenTags')->willReturn(['test','spam']); $mockSearch->disableFilter(); $manager = new Manager\Collage(); $initial = sizeof($mockSearch->setLookup('name')->page(100, 0)); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage add ' . randomString(20), description: 'phpunit collage add description', tagList: 'jazz metal rap', ); $this->collageList[] = $manager->create( user: $this->userList['u1'], categoryId: CollageType::theme->value, name: 'phpunit collage add ' . randomString(20), description: 'phpunit collage add description', tagList: 'funk spam rock', ); $result = $mockSearch->page(100, 0); $this->assertCount($initial + 2, $result, 'collage-search-results'); } 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->total(), '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'); $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->total(), '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-bookmark' ); $payload = new Json\Collage( $collage, 1, $this->userList['u1'], )->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->isLockedForUser($this->userList['u1']), 'collage-is-not-locked' ); $this->assertTrue( $collage->toggleLocked(true)->modify(), 'collage-lock' ); $this->assertTrue( $collage->isLockedForUser($this->userList['u1']), 'collage-is-now-locked' ); $this->userList['u2']->addCustomPrivilege('site_collages_manage'); $this->assertFalse( $collage->isLockedForUser($this->userList['u2']), 'collage-is-manager-unlocked' ); $this->assertFalse( $collage->userCanContribute($this->userList['u1']), 'collage-locked-contribute' ); $this->assertTrue( $collage->toggleLocked(false)->modify(), 'collage-unlock' ); $this->assertFalse( $collage->isLockedForUser($this->userList['u1']), '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 testCollageComment(): 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]; $commentMan = new Manager\Comment(); $comment = $commentMan->create( $this->userList['u1'], 'collages', $collage->id, 'phpunit collage comment ' . randomString(), ); $summary = $commentMan->collageSummary($collage); $this->assertCount(1, $summary, 'collage-comment-summary-count'); $first = current($summary); $this->assertEquals( ['id', 'body', 'author_id', 'added'], array_keys($first), 'collage-comment-summary-entry', ); $this->assertEquals(1, $comment->remove(), 'collage-comment-remove'); } 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(true)->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->total(), $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' ); } public function testCollageSearchComment(): void { /** * Strategy: * - u1 creates a collage * - u2 adds a collage to it * - u3 comments on the collage. * This way all of the use cases can be covered */ $manager = new Manager\Collage(); $creator = $this->userList['u1']; $adder = $this->userList['u2']; $writer = $this->userList['u3']; $this->collageList[] = $manager->create( user: $creator, categoryId: CollageType::theme->value, name: 'phpunit collage comment ' . randomString(20), description: 'phpunit collage comment description', tagList: implode(' ', $this->tagList(3)), ); $collage = $this->collageList[0]; $body = "phpunit u{$writer->id} c{$collage->id}"; $this->tgroupList = [ Helper::makeTGroupMusic( $adder, 'phpunit collage comment ' . randomString(8), [[ARTIST_MAIN], ['phpunit collage comment ' . randomString(6)]], $this->tagList(1), ), ]; $collage->addEntry($this->tgroupList[0], $adder, 'collage-add-dupe-entry'); $comment = new Manager\Comment()->create( $writer, CommentType::collage->value, $collage->id, $body, ); $searchCreator = new Search\Comment( CommentType::collage, 'default', $creator->id !== $creator->id, $creator, ); $this->assertEquals( 0, $searchCreator->total(), 'collage-comment-creator-total', ); $this->assertEquals( 'Collage comments', $searchCreator->title(), 'collage-comment-creator-title', ); $searchCreatorCreated = new Search\Comment( CommentType::collage, 'created', $creator->id === $creator->id, $creator, ); $this->assertEquals( 1, $searchCreatorCreated->total(), 'collage-comment-creator-created-total', ); $this->assertEqualsCanonicalizing( [ "Collage comments", "Contributed collage comments", ], array_map( fn ($t) => $t['title'], $searchCreatorCreated->linkList(), ), 'collage-comment-creator-created-linklist', ); $page = $searchCreatorCreated->page(2, 0); $this->assertCount(1, $page['list'], 'collage-comment-creator-created-list-total'); $this->assertCount(1, $page['user'], 'collage-comment-creator-created-user-total'); $this->assertTrue( isset($page['user'][$writer->id]), 'collage-comment-creator-created-id', ); $comment = $page['list'][0]; $this->assertEquals($body, $comment['body'], 'collage-comment-writer-body'); $searchCreatorContributed = new Search\Comment( CommentType::collage, 'contributed', $creator->id === $creator->id, $creator, ); $this->assertEquals( 0, $searchCreatorContributed->total(), 'collage-comment-creator-contributed-total', ); $this->assertEqualsCanonicalizing( [ "Collage comments", "Comments on your collages", ], array_map( fn ($t) => $t['title'], $searchCreatorContributed->linkList(), ), 'collage-comment-creator-contributed-linklist', ); $searchContributor = new Search\Comment( CommentType::collage, 'contributed', $adder->id === $adder->id, $adder, ); $this->assertEquals( 'Contributed collage comments', $searchContributor->title(), 'collage-comment-contributor-linklist', ); $this->assertEquals( 1, $searchContributor->total(), 'collage-comment-contributor-total', ); $page = $searchContributor->page(2, 0); $this->assertCount(1, $page['list'], 'collage-comment-contributor-list-total'); $this->assertCount(1, $page['user'], 'collage-comment-contributor-user-total'); $this->assertTrue( isset($page['user'][$writer->id]), 'collage-comment-contributor-id', ); $comment = $page['list'][0]; $this->assertEquals($body, $comment['body'], 'collage-comment-contributor-writer-body'); $searchWriter = new Search\Comment( CommentType::collage, 'default', $writer->id === $writer->id, $writer, ); $this->assertEquals( 'Collage comments', $searchWriter->title(), 'collage-comment-writer-linklist', ); $this->assertEquals( 1, $searchWriter->total(), 'collage-comment-writer-total', ); $page = $searchWriter->page(2, 0); $this->assertCount(1, $page['list'], 'collage-comment-writer-list-total'); $this->assertCount(1, $page['user'], 'collage-comment-writer-user-total'); $this->assertTrue( isset($page['user'][$writer->id]), 'collage-comment-writer-writer-id', ); $comment = $page['list'][0]; $this->assertEquals($body, $comment['body'], 'collage-comment-writer-writer-body'); $searchWriterOther = new Search\Comment( CommentType::collage, 'default', $writer->id !== $writer->id, $writer, ); $this->assertEqualsCanonicalizing( [ "Contributed collage comments", "Comments on their collages", ], array_map( fn ($t) => $t['title'], $searchWriterOther->linkList() ), 'collage-comment-writer-other-linklist', ); $searchWriterCreated = new Search\Comment( CommentType::collage, 'created', $writer->id === $writer->id, $writer, ); $this->assertEquals( 0, $searchWriterCreated->total(), 'collage-comment-writer-created-total', ); $this->assertEquals( 'Comments on your collages', $searchWriterCreated->title(), 'collage-comment-writer-created-title', ); $searchWriterContributed = new Search\Comment( CommentType::collage, 'contributed', $writer->id === $writer->id, $writer, ); $this->assertEquals( 0, $searchWriterContributed->total(), 'collage-comment-writer-contributed-total', ); } }