073f470b4f17c04ae4f009a796e393ef47fd7ac9
[GitHub/WoltLab/WCF.git] /
1 <?php
2 declare(strict_types=1);
3 namespace wcf\system\user\notification\event;
4 use wcf\data\comment\response\CommentResponse;
5 use wcf\data\comment\response\CommentResponseAction;
6 use wcf\data\comment\Comment;
7 use wcf\data\comment\CommentAction;
8 use wcf\data\comment\CommentEditor;
9 use wcf\data\object\type\ObjectTypeCache;
10 use wcf\data\user\UserProfile;
11 use wcf\system\comment\manager\ICommentManager;
12 use wcf\system\user\notification\object\CommentResponseUserNotificationObject;
13 use wcf\system\user\notification\object\IUserNotificationObject;
14
15 /**
16 * Default implementation of some methods of the testable user notification event interface
17 * for comment response user notificiation events.
18 *
19 * As PHP 5.5 does not support abstract static functions in traits, we require them by this documentation:
20 * - protected static function getTestCommentObjectData(UserProfile $recipient, UserProfile $author)
21 * returns the `objectID` and `objectTypeID` parameter for comment creation.
22 *
23 * @author Matthias Schmidt
24 * @copyright 2001-2018 WoltLab GmbH
25 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
26 * @package WoltLabSuite\Core\System\User\Notification\Event
27 * @since 3.1
28 */
29 trait TTestableCommentResponseUserNotificationEvent {
30 use TTestableUserNotificationEvent;
31
32 /**
33 * @inheritDoc
34 */
35 public static function canBeTriggeredByGuests() {
36 return true;
37 }
38
39 /**
40 * Creates a test comment response.
41 *
42 * @param UserProfile $recipient
43 * @param UserProfile $author
44 * @return CommentResponse
45 */
46 public static function createTestCommentResponse(UserProfile $recipient, UserProfile $author) {
47 /** @var Comment $comment */
48 $comment = (new CommentAction([], 'create', [
49 'data' => array_merge([
50 'enableHtml' => 1,
51 'isDisabled' => 0,
52 'message' => '<p>Test Comment</p>',
53 'time' => TIME_NOW - 10,
54 'userID' => $recipient->userID,
55 'username' => $recipient->username
56 ], self::getTestCommentObjectData($recipient, $author))
57 ]))->executeAction()['returnValues'];
58
59 /** @var ICommentManager $commentManager */
60 $commentManager = ObjectTypeCache::getInstance()->getObjectType($comment->objectTypeID)->getProcessor();
61 $commentManager->updateCounter($comment->objectID, 1);
62
63 /** @var CommentResponse $commentResponse */
64 $commentResponse = (new CommentResponseAction([], 'create', [
65 'data' => [
66 'commentID' => $comment->commentID,
67 'time' => TIME_NOW - 10,
68 'userID' => $author->userID,
69 'username' => $author->username,
70 'message' => 'Test Response',
71 'isDisabled' => 0
72 ]
73 ]))->executeAction()['returnValues'];
74
75 $commentResponse->setComment($comment);
76
77 $commentEditor = new CommentEditor($comment);
78 $commentEditor->updateResponseIDs();
79 $commentEditor->updateUnfilteredResponseIDs();
80
81 return $commentResponse;
82 }
83
84 /**
85 * @inheritDoc
86 */
87 public static function getTestAdditionalData(IUserNotificationObject $object) {
88 /** @var CommentResponseUserNotificationObject $object */
89
90 return [
91 'commentID' => $object->commentID,
92 'objectID' => $object->getComment()->objectID,
93 'objectUserID' => $object->getComment()->objectID,
94 'userID' => $object->getComment()->userID
95 ];
96 }
97
98 /**
99 * @inheritDoc
100 * @return CommentResponseUserNotificationObject[]
101 */
102 public static function getTestObjects(UserProfile $recipient, UserProfile $author) {
103 return [new CommentResponseUserNotificationObject(self::createTestCommentResponse($recipient, $author))];
104 }
105 }