Add notification tests to developer tools
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / user / notification / event / UserTrophyReceivedNotificationEvent.class.php
1 <?php
2 namespace wcf\system\user\notification\event;
3 use wcf\data\category\CategoryAction;
4 use wcf\data\trophy\category\TrophyCategory;
5 use wcf\data\trophy\category\TrophyCategoryCache;
6 use wcf\data\trophy\Trophy;
7 use wcf\data\trophy\TrophyAction;
8 use wcf\data\trophy\TrophyCache;
9 use wcf\data\user\trophy\UserTrophy;
10 use wcf\data\user\trophy\UserTrophyAction;
11 use wcf\data\user\UserProfile;
12 use wcf\system\cache\builder\TrophyCacheBuilder;
13 use wcf\system\category\CategoryHandler;
14 use wcf\system\user\notification\object\UserTrophyNotificationObject;
15
16 /**
17 * Notification event for receiving a user trophy.
18 *
19 * @author Joshua Ruesweg
20 * @copyright 2001-2017 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\System\User\Notification\Object\Type
23 *
24 * @method UserTrophyNotificationObject getUserNotificationObject()
25 */
26 class UserTrophyReceivedNotificationEvent extends AbstractUserNotificationEvent implements ITestableUserNotificationEvent {
27 use TTestableUserNotificationEvent;
28
29 /**
30 * @inheritDoc
31 */
32 public function getTitle() {
33 return $this->getLanguage()->get('wcf.user.notification.trophy.received.title');
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function getMessage() {
40 return $this->getLanguage()->getDynamicVariable('wcf.user.notification.trophy.received.message', [
41 'userTrophy' => $this->userNotificationObject,
42 'author' => $this->author
43 ]);
44 }
45
46 /**
47 * @inheritDoc
48 */
49 public function supportsEmailNotification() {
50 return false;
51 }
52
53 /**
54 * @inheritDoc
55 */
56 public function getLink() {
57 return $this->getUserNotificationObject()->getTrophy()->getLink();
58 }
59
60 /**
61 * @inheritDoc
62 */
63 public function checkAccess() {
64 return $this->getUserNotificationObject()->getDecoratedObject()->canSee();
65 }
66
67 /**
68 * @inheritDoc
69 * @return UserTrophyNotificationObject[]
70 * @since 3.1
71 */
72 public static function getTestObjects(UserProfile $recipient, UserProfile $author) {
73 /** @var TrophyCategory $trophyCategory */
74 $trophyCategory = null;
75 $trophyCategories = TrophyCategoryCache::getInstance()->getEnabledCategories();
76
77 if (empty($trophyCategories)) {
78 $trophyCategory = (new CategoryAction([], 'create', [
79 'data' => [
80 'description' => 'Trophy Category Description',
81 'isDisabled' => 0,
82 'objectTypeID' => CategoryHandler::getInstance()->getObjectType(TrophyCategory::OBJECT_TYPE_NAME)->objectTypeID,
83 'title' => 'Trophy Category'
84 ]
85 ]))->executeAction()['returnValues'];
86 }
87 else {
88 $trophyCategory = reset($trophyCategories);
89 }
90
91 /** @var Trophy $trophy */
92 $trophy = (new TrophyAction([], 'create', [
93 'data' => [
94 'title' => 'Trophy Title',
95 'description' => 'Trophy Description',
96 'categoryID' => $trophyCategory->categoryID,
97 'type' => Trophy::TYPE_BADGE,
98 'isDisabled' => 0,
99 'awardAutomatically' => 0,
100 'iconName' => 'trophy',
101 'iconColor' => 'rgba(255, 255, 255, 1)',
102 'badgeColor' => 'rgba(50, 92, 132, 1)'
103 ]
104 ]))->executeAction()['returnValues'];
105
106 /** @var UserTrophy $userTropy */
107 $userTropy = (new UserTrophyAction([], 'create', [
108 'data' => [
109 'trophyID' => $trophy->trophyID,
110 'userID' => $recipient->userID,
111 'description' => 'User Trophy Description',
112 'time' => TIME_NOW,
113 'useCustomDescription' => 1
114 ]
115 ]))->executeAction()['returnValues'];
116
117 TrophyCache::getInstance()->clearCache();
118
119 // work-around to reset trophy cache during this request
120 $reflectionClass = new \ReflectionClass(TrophyCacheBuilder::class);
121 $reflectionProperty = $reflectionClass->getProperty('cache');
122 $reflectionProperty->setAccessible(true);
123 $reflectionProperty->setValue(TrophyCacheBuilder::getInstance(), []);
124
125 return [new UserTrophyNotificationObject($userTropy)];
126 }
127 }