1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Models;
7
8use App\Models\Group;
9use App\Models\UserGroupEvent;
10use Tests\TestCase;
11
12class GroupTest extends TestCase
13{
14 public function testRename()
15 {
16 $newName = 'new name';
17 $group = Group::factory()->create(['group_name' => 'name']);
18 $groupRenameEventCount = $this->getGroupRenameEventCount($group);
19
20 $group->rename($newName);
21
22 $this->assertSame($group->group_name, $newName);
23 $this->assertSame($this->getGroupRenameEventCount($group), $groupRenameEventCount + 1);
24 }
25
26 public function testRenameUnchanged()
27 {
28 $name = 'name';
29 $group = Group::factory()->create(['group_name' => $name]);
30 $groupRenameEventCount = $this->getGroupRenameEventCount($group);
31
32 $group->rename($name);
33
34 $this->assertSame($this->getGroupRenameEventCount($group), $groupRenameEventCount);
35 }
36
37 /*
38 TODO: This test always fails because it depends on `Group::afterCommit`
39 being called right after `create`, but the whole test is wrapped in a
40 transaction.
41
42 public function testResetCacheOnSave()
43 {
44 $previousCacheVersion = cache()->get('groups_local_cache_version');
45
46 Group::create(['group_desc' => '']);
47
48 $this->assertNotSame($previousCacheVersion, cache()->get('groups_local_cache_version'));
49 }
50 */
51
52 private function getGroupRenameEventCount(Group $group): int
53 {
54 return UserGroupEvent::where([
55 'group_id' => $group->getKey(),
56 'type' => UserGroupEvent::GROUP_RENAME,
57 ])->count();
58 }
59}