diff --git a/lib/Controller/APIv1.php b/lib/Controller/APIv1.php index 692b3aba7..572572f36 100644 --- a/lib/Controller/APIv1.php +++ b/lib/Controller/APIv1.php @@ -24,7 +24,6 @@ use OCA\Activity\CurrentUser; use OCA\Activity\Data; use OCA\Activity\GroupHelper; -use OCA\Activity\PlainTextParser; use OCA\Activity\UserSettings; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; @@ -41,9 +40,6 @@ class APIv1 extends OCSController { /** @var UserSettings */ protected $userSettings; - /** @var PlainTextParser */ - protected $parser; - /** @var CurrentUser */ protected $currentUser; @@ -53,7 +49,6 @@ class APIv1 extends OCSController { * @param Data $data * @param GroupHelper $groupHelper * @param UserSettings $userSettings - * @param PlainTextParser $parser * @param CurrentUser $currentUser */ public function __construct($appName, @@ -61,14 +56,12 @@ public function __construct($appName, Data $data, GroupHelper $groupHelper, UserSettings $userSettings, - PlainTextParser $parser, CurrentUser $currentUser) { parent::__construct($appName, $request); $this->data = $data; $this->userSettings = $userSettings; $this->groupHelper = $groupHelper; - $this->parser = $parser; $this->currentUser = $currentUser; } diff --git a/lib/Controller/Feed.php b/lib/Controller/Feed.php index f773630b7..273f12e1d 100644 --- a/lib/Controller/Feed.php +++ b/lib/Controller/Feed.php @@ -24,7 +24,6 @@ use OCA\Activity\Data; use OCA\Activity\GroupHelper; -use OCA\Activity\PlainTextParser; use OCA\Activity\UserSettings; use OCP\Activity\IManager; use OCP\AppFramework\Controller; @@ -108,9 +107,7 @@ public function show() { // Overwrite user and language in the helper $this->l = $this->l10nFactory->get('activity', $userLang); - $parser = new PlainTextParser($this->l); $this->helper->setL10n($this->l); - $this->helper->setUser($user); $description = (string) $this->l->t('Personal activity feed for %s', $user); $response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all'); diff --git a/lib/Data.php b/lib/Data.php index 6f61de86d..456b0d30f 100755 --- a/lib/Data.php +++ b/lib/Data.php @@ -158,7 +158,6 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, if ($user === '') { throw new \OutOfBoundsException('Invalid user', 1); } - $groupHelper->setUser($user); $activeFilter = null; try { diff --git a/lib/DataHelper.php b/lib/DataHelper.php deleted file mode 100644 index 982220f31..000000000 --- a/lib/DataHelper.php +++ /dev/null @@ -1,202 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity; - -use OCA\Activity\Parameter\Factory; -use OCA\Activity\Parameter\IParameter; -use OCA\Activity\Parameter\Collection; -use OCP\Activity\IEvent; -use OCP\Activity\IManager; -use OCP\IL10N; -use OCP\L10N\IFactory; - -class DataHelper { - /** @var \OCP\Activity\IManager */ - protected $activityManager; - - /** @var \OCA\Activity\Parameter\Factory */ - protected $parameterFactory; - - /** @var IFactory */ - protected $l10Nfactory; - - /** @var IL10N */ - protected $l; - - /** - * @param IManager $activityManager - * @param Factory $parameterFactory - * @param IFactory $l10Nfactory - * @param IL10N $l - */ - public function __construct(IManager $activityManager, Factory $parameterFactory, IFactory $l10Nfactory, IL10N $l) { - $this->activityManager = $activityManager; - $this->parameterFactory = $parameterFactory; - $this->l10Nfactory = $l10Nfactory; - $this->l = $l; - } - - /** - * @param string $user - */ - public function setUser($user) { - $this->parameterFactory->setUser($user); - } - - /** - * @param IL10N $l - */ - public function setL10n(IL10N $l) { - $this->parameterFactory->setL10n($l); - $this->l = $l; - } - - /** - * @brief Translate an event string with the translations from the app where it was send from - * @param string $app The app where this event comes from - * @param string $text The text including placeholders - * @param IParameter[] $params The parameter for the placeholder - * @return string translated - */ - public function translation($app, $text, array $params) { - if (!$text) { - return ''; - } - - $preparedParams = []; - foreach ($params as $parameter) { - $preparedParams[] = $parameter->format(); - } - - // Allow apps to correctly translate their activities - $translation = $this->activityManager->translate( - $app, $text, $preparedParams, false, false, $this->l->getLanguageCode()); - - if ($translation !== false) { - return $translation; - } - - $l = $this->l10Nfactory->get($app, $this->l->getLanguageCode()); - return $l->t($text, $preparedParams); - } - - /** - * List with special parameters for the message - * - * @param string $app - * @param string $text - * @return array - */ - protected function getSpecialParameterList($app, $text) { - $specialParameters = $this->activityManager->getSpecialParameterList($app, $text); - - if ($specialParameters !== false) { - return $specialParameters; - } - - return array(); - } - - /** - * Format strings for display - * - * @param array $activity - * @param string $message 'subject' or 'message' - * @return array Modified $activity - */ - public function formatStrings($activity, $message) { - $activity[$message . 'params'] = $activity[$message . 'params_array']; - unset($activity[$message . 'params_array']); - $activity[$message . '_prepared'] = $this->translation($activity['app'], $activity[$message], $activity[$message . 'params']); - - return $activity; - } - - /** - * Get the parameter array from the parameter string of the database table - * - * @param IEvent $event - * @param string $parsing What are we parsing `message` or `subject` - * @param string $parameterString can be a JSON string, serialize() or a simple string. - * @return array List of Parameters - */ - public function getParameters(IEvent $event, $parsing, $parameterString) { - $parameters = $this->parseParameters($parameterString); - $parameterTypes = $this->getSpecialParameterList( - $event->getApp(), - ($parsing === 'subject') ? $event->getSubject() : $event->getMessage() - ); - - foreach ($parameters as $i => $parameter) { - $parameters[$i] = $this->parameterFactory->get( - $parameter, - $event, - isset($parameterTypes[$i]) ? $parameterTypes[$i] : 'base' - ); - } - - return $parameters; - } - - /** - * @return Collection - */ - public function createCollection() { - return $this->parameterFactory->createCollection(); - } - - /** - * Get the parameter array from the parameter string of the database table - * - * @param string $parameterString can be a JSON string, serialize() or a simple string. - * @return array List of Parameters - */ - public function parseParameters($parameterString) { - if (!is_string($parameterString)) { - return []; - } - $parameters = $parameterString; - - if ($parameterString[0] === '[' && substr($parameterString, -1) === ']' || $parameterString[0] === '"' && substr($parameterString, -1) === '"') { - // ownCloud 8.1+ - $parameters = json_decode($parameterString, true); - if ($parameters === null) { - // Error on json decode - $parameters = $parameterString; - } - - } else if (isset($parameterString[7]) && $parameterString[1] === ':' && ($parameterString[0] === 's' && substr($parameterString, -1) === ';' || $parameterString[0] === 'a' && substr($parameterString, -1) === '}')) { - // ownCloud 7+ - // Min length 8: `s:1:"a";` - // Accepts: `s:1:"a";` for single string `a:1:{i:0;s:1:"a";}` for array - $parameters = unserialize($parameterString); - } - - if (is_array($parameters)) { - return $parameters; - } - - // ownCloud <7 - return [$parameters]; - } -} diff --git a/lib/Extension/LegacyParser.php b/lib/Extension/LegacyParser.php deleted file mode 100644 index 976b594fa..000000000 --- a/lib/Extension/LegacyParser.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -namespace OCA\Activity\Extension; - - -use OCA\Activity\DataHelper; -use OCA\Activity\PlainTextParser; -use OCP\Activity\IEvent; -use OCP\Activity\IProvider; -use OCP\L10N\IFactory; - -class LegacyParser implements IProvider { - - /** @var IFactory */ - protected $languageFactory; - - /** @var DataHelper */ - protected $dataHelper; - - /** @var PlainTextParser */ - protected $parser; - - /** - * @param IFactory $languageFactory - * @param DataHelper $dataHelper - * @param PlainTextParser $parser - */ - public function __construct(IFactory $languageFactory, DataHelper $dataHelper, PlainTextParser $parser) { - $this->languageFactory = $languageFactory; - $this->dataHelper = $dataHelper; - $this->parser = $parser; - } - - /** - * @param string $language - * @param IEvent $event - * @param IEvent|null $previousEvent - * @return IEvent - * @throws \InvalidArgumentException - * @since 9.2.0 - */ - public function parse($language, IEvent $event, IEvent $previousEvent = null) { - $l = $this->languageFactory->get('activity', $language); - $this->dataHelper->setL10n($l); - - $event->setParsedSubject($this->parser->parseMessage($this->dataHelper->translation( - $event->getApp(), - $event->getSubject(), - $this->dataHelper->getParameters($event, 'subject', json_encode($event->getSubjectParameters())) - ))); - - $event->setParsedMessage($this->parser->parseMessage($this->dataHelper->translation( - $event->getApp(), - $event->getMessage(), - $this->dataHelper->getParameters($event, 'message', json_encode($event->getMessageParameters())) - ))); - - return $event; - } -} diff --git a/lib/Formatter/BaseFormatter.php b/lib/Formatter/BaseFormatter.php deleted file mode 100644 index 0f4096a8f..000000000 --- a/lib/Formatter/BaseFormatter.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Formatter; - -use OCP\Activity\IEvent; -use OCP\Util; - -class BaseFormatter implements IFormatter { - /** - * @param IEvent $event - * @param string $parameter The parameter to be formatted - * @return string The formatted parameter - */ - public function format(IEvent $event, $parameter) { - return '' . Util::sanitizeHTML($parameter) . ''; - } -} diff --git a/lib/Formatter/CloudIDFormatter.php b/lib/Formatter/CloudIDFormatter.php deleted file mode 100644 index 8fe4e458e..000000000 --- a/lib/Formatter/CloudIDFormatter.php +++ /dev/null @@ -1,102 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Formatter; - -use OC\HintException; -use OC\Share\Helper; -use OCP\Activity\IEvent; -use OCP\Contacts\IManager; -use OCP\Util; - -class CloudIDFormatter implements IFormatter { - /** @var IManager */ - protected $manager; - /** @var array */ - protected $federatedContacts; - - /** - * @param IManager $contactsManager - */ - public function __construct(IManager $contactsManager) { - $this->manager = $contactsManager; - $this->federatedContacts = []; - } - - /** - * @param IEvent $event - * @param string $parameter The parameter to be formatted - * @return string The formatted parameter - */ - public function format(IEvent $event, $parameter) { - $displayName = $parameter; - try { - list($user, $server) = Helper::splitUserRemote($parameter); - } catch (HintException $e) { - $user = $parameter; - $server = ''; - } - - if ($server !== '') { - $displayName = $user . '@…'; - } - - try { - $displayName = $this->getDisplayNameFromContact($parameter); - } catch (\OutOfBoundsException $e) {} - - return '' . Util::sanitizeHTML($parameter) . ''; - } - - /** - * Try to find the user in the contacts - * - * @param string $federatedCloudId - * @return string - * @throws \OutOfBoundsException when there is no contact for the id - */ - protected function getDisplayNameFromContact($federatedCloudId) { - $federatedCloudId = strtolower($federatedCloudId); - if (isset($this->federatedContacts[$federatedCloudId])) { - if ($this->federatedContacts[$federatedCloudId] !== '') { - return $this->federatedContacts[$federatedCloudId]; - } else { - throw new \OutOfBoundsException('No contact found for federated cloud id'); - } - } - - $addressBookEntries = $this->manager->search($federatedCloudId, ['CLOUD']); - foreach ($addressBookEntries as $entry) { - if (isset($entry['CLOUD'])) { - foreach ($entry['CLOUD'] as $cloudID) { - if ($cloudID === $federatedCloudId) { - $this->federatedContacts[$federatedCloudId] = $entry['FN']; - return $entry['FN']; - } - } - } - } - - $this->federatedContacts[$federatedCloudId] = ''; - throw new \OutOfBoundsException('No contact found for federated cloud id'); - } -} diff --git a/lib/Formatter/FileFormatter.php b/lib/Formatter/FileFormatter.php deleted file mode 100644 index 79f9b5258..000000000 --- a/lib/Formatter/FileFormatter.php +++ /dev/null @@ -1,137 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Formatter; - -use OCA\Activity\ViewInfoCache; -use OCP\Activity\IEvent; -use OCP\IL10N; -use OCP\IURLGenerator; -use OCP\Util; - -class FileFormatter implements IFormatter { - /** @var ViewInfoCache */ - protected $infoCache; - /** @var IURLGenerator */ - protected $urlGenerator; - /** @var IL10N */ - protected $l; - /** @var string */ - protected $user; - - /** - * @param ViewInfoCache $infoCache - * @param IURLGenerator $urlGenerator - * @param IL10N $l - */ - public function __construct(ViewInfoCache $infoCache, IURLGenerator $urlGenerator, IL10N $l) { - $this->infoCache = $infoCache; - $this->urlGenerator = $urlGenerator; - $this->l = $l; - } - - /** - * @param string $user - */ - public function setUser($user) { - $this->user = (string) $user; - } - - /** - * @param IEvent $event - * @param string $parameter The parameter to be formatted - * @return string The formatted parameter - */ - public function format(IEvent $event, $parameter) { - $param = $this->fixLegacyFilename($parameter); - - // If the activity is about the very same file, we use the current path - // for the link generation instead of the one that was saved. - $fileId = ''; - if (is_array($param)) { - $fileId = key($param); - $param = $param[$fileId]; - $info = $this->infoCache->getInfoById($this->user, $fileId, $param); - } elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { - $fileId = $event->getObjectId(); - $info = $this->infoCache->getInfoById($this->user, $fileId, $param); - } else { - $info = $this->infoCache->getInfoByPath($this->user, $param); - } - - if ($info['is_dir']) { - $linkData = ['dir' => $info['path']]; - } else { - $parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']); - $fileName = basename($info['path']); - $linkData = [ - 'dir' => $parentDir, - 'scrollto' => $fileName, - ]; - } - - if ($info['view'] !== '') { - $linkData['view'] = $info['view']; - } - - $param = trim($param, '/'); - if ($param === '') { - $param = '/'; - } - - $fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData); - - return '' . Util::sanitizeHTML($param) . ''; - } - - /** - * Prepend leading slash to filenames of legacy activities - * @param string|array $filename - * @return string|array - */ - protected function fixLegacyFilename($filename) { - if (is_array($filename)) { - // 9.0: [fileId => path] - return $filename; - } - if (strpos($filename, '/') !== 0) { - return '/' . $filename; - } - return $filename; - } - - /** - * Split the path from the filename string - * - * @param string $filename - * @return array Array with path and filename - */ - protected function splitPathFromFilename($filename) { - if (strrpos($filename, '/') !== false) { - return array( - trim(substr($filename, 0, strrpos($filename, '/')), '/'), - substr($filename, strrpos($filename, '/') + 1), - ); - } - return array('', $filename); - } -} diff --git a/lib/Formatter/IFormatter.php b/lib/Formatter/IFormatter.php deleted file mode 100644 index d4112f0b6..000000000 --- a/lib/Formatter/IFormatter.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Formatter; - -use OCP\Activity\IEvent; - -interface IFormatter { - /** - * @param IEvent $event - * @param string $parameter The parameter to be formatted - * @return string The formatted parameter - */ - public function format(IEvent $event, $parameter); -} diff --git a/lib/Formatter/UserFormatter.php b/lib/Formatter/UserFormatter.php deleted file mode 100644 index 1b5332e25..000000000 --- a/lib/Formatter/UserFormatter.php +++ /dev/null @@ -1,89 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Formatter; - -use OCP\Activity\IEvent; -use OCP\IL10N; -use OCP\IUser; -use OCP\IUserManager; -use OCP\Util; - -class UserFormatter implements IFormatter { - - /** @var IUserManager */ - protected $manager; - - /** @var IL10N */ - protected $l; - - /** @var CloudIDFormatter */ - protected $cloudIDFormatter; - - /** - * @param IUserManager $userManager - * @param CloudIDFormatter $cloudIDFormatter - * @param IL10N $l - */ - public function __construct(IUserManager $userManager, CloudIDFormatter $cloudIDFormatter, IL10N $l) { - $this->manager = $userManager; - $this->l = $l; - $this->cloudIDFormatter = $cloudIDFormatter; - } - - /** - * @param IEvent $event - * @param string $parameter The parameter to be formatted - * @return string The formatted parameter - */ - public function format(IEvent $event, $parameter) { - // If the username is empty, the action has been performed by a remote - // user, or via a public share. We don't know the username in that case - if ($parameter === '') { - return '' . Util::sanitizeHTML('') . ''; - } - - $user = $this->manager->get($parameter); - if (!($user instanceof IUser)) { - if ($this->isRemoteUser($parameter)) { - // Remote user detected - return $this->cloudIDFormatter->format($event, $parameter); - } - $displayName = $parameter; - } else { - $displayName = $user->getDisplayName(); - } - $parameter = Util::sanitizeHTML($parameter); - - return '' . Util::sanitizeHTML($parameter) . ''; - } - - /** - * Very simple "remote user" detection should be improved someday™ - * - * @param string $parameter - * @return bool - */ - protected function isRemoteUser($parameter) { - return strpos($parameter, '@') > 0; - } -} diff --git a/lib/GroupHelper.php b/lib/GroupHelper.php index 38c9da985..b55aabf9f 100644 --- a/lib/GroupHelper.php +++ b/lib/GroupHelper.php @@ -22,8 +22,6 @@ namespace OCA\Activity; -use OCA\Activity\Extension\LegacyParser; -use OCA\Activity\Parameter\IParameter; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\IL10N; @@ -46,39 +44,22 @@ class GroupHelper { /** @var \OCP\Activity\IManager */ protected $activityManager; - /** @var \OCA\Activity\DataHelper */ - protected $dataHelper; - /** @var IValidator */ protected $richObjectValidator; /** @var ILogger */ protected $logger; - /** @var LegacyParser */ - protected $legacyParser; - public function __construct(IL10N $l, IManager $activityManager, - DataHelper $dataHelper, IValidator $richObjectValidator, - ILogger $logger, - LegacyParser $legacyParser) { + ILogger $logger) { $this->allowGrouping = true; $this->l = $l; $this->activityManager = $activityManager; - $this->dataHelper = $dataHelper; $this->richObjectValidator = $richObjectValidator; $this->logger = $logger; - $this->legacyParser = $legacyParser; - } - - /** - * @param string $user - */ - public function setUser($user) { - $this->dataHelper->setUser($user); } /** @@ -86,7 +67,6 @@ public function setUser($user) { */ public function setL10n(IL10N $l) { $this->l = $l; - $this->dataHelper->setL10n($l); } /** @@ -136,14 +116,8 @@ public function addActivity($activity) { } if (!$event->getParsedSubject()) { - try { - $this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId()); - $event = $this->legacyParser->parse($language, $event); - $this->activityManager->setFormattingObject('', 0); - } catch (\InvalidArgumentException $e) { - \OC::$server->getLogger()->debug('Failed to parse activity'); - return; - } + $this->logger->debug('Activity "' . $event->getRichSubject() . '" was not parsed by any provider'); + return; } $this->event[$id] = $event; diff --git a/lib/GroupHelperDisabled.php b/lib/GroupHelperDisabled.php index 86a1c42b1..9bdac201f 100644 --- a/lib/GroupHelperDisabled.php +++ b/lib/GroupHelperDisabled.php @@ -21,7 +21,6 @@ namespace OCA\Activity; -use OCA\Activity\Extension\LegacyParser; use OCP\Activity\IManager; use OCP\IL10N; use OCP\ILogger; @@ -29,24 +28,14 @@ class GroupHelperDisabled extends GroupHelper { - /** - * @param IL10N $l - * @param IManager $activityManager - * @param DataHelper $dataHelper - * @param LegacyParser $legacyParser - */ public function __construct(IL10N $l, IManager $activityManager, - DataHelper $dataHelper, IValidator $richObjectValidator, - ILogger $logger, - LegacyParser $legacyParser) { + ILogger $logger) { parent::__construct($l, - $activityManager, - $dataHelper, - $richObjectValidator, - $logger, - $legacyParser); + $activityManager, + $richObjectValidator, + $logger); $this->allowGrouping = false; } } diff --git a/lib/MailQueueHandler.php b/lib/MailQueueHandler.php index d7f4efd71..fe76a963f 100644 --- a/lib/MailQueueHandler.php +++ b/lib/MailQueueHandler.php @@ -23,7 +23,6 @@ namespace OCA\Activity; -use OCA\Activity\Extension\LegacyParser; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -37,6 +36,7 @@ use OCP\IUserManager; use OCP\L10N\IFactory; use OCP\Mail\IMailer; +use OCP\RichObjectStrings\IValidator; use OCP\Util; /** @@ -66,9 +66,6 @@ class MailQueueHandler { /** @var IDateTimeFormatter */ protected $dateFormatter; - /** @var DataHelper */ - protected $dataHelper; - /** @var IDBConnection */ protected $connection; @@ -87,8 +84,8 @@ class MailQueueHandler { /** @var IManager */ protected $activityManager; - /** @var LegacyParser */ - protected $legacyParser; + /** @var IValidator */ + protected $richObjectValidator; /** @var IConfig */ protected $config; @@ -96,41 +93,24 @@ class MailQueueHandler { /** @var ILogger */ protected $logger; - /** - * Constructor - * - * @param IDateTimeFormatter $dateFormatter - * @param IDBConnection $connection - * @param DataHelper $dataHelper - * @param IMailer $mailer - * @param IURLGenerator $urlGenerator - * @param IUserManager $userManager - * @param IFactory $lFactory - * @param IManager $activityManager - * @param LegacyParser $legacyParser - * @param IConfig $config - * @param ILogger $logger - */ public function __construct(IDateTimeFormatter $dateFormatter, IDBConnection $connection, - DataHelper $dataHelper, IMailer $mailer, IURLGenerator $urlGenerator, IUserManager $userManager, IFactory $lFactory, IManager $activityManager, - LegacyParser $legacyParser, + IValidator $richObjectValidator, IConfig $config, ILogger $logger) { $this->dateFormatter = $dateFormatter; $this->connection = $connection; - $this->dataHelper = $dataHelper; $this->mailer = $mailer; $this->urlGenerator = $urlGenerator; $this->userManager = $userManager; $this->lFactory = $lFactory; $this->activityManager = $activityManager; - $this->legacyParser = $legacyParser; + $this->richObjectValidator = $richObjectValidator; $this->config = $config; $this->logger = $logger; } @@ -347,8 +327,6 @@ protected function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime); $l = $this->getLanguage($lang); - $this->dataHelper->setUser($userName); - $this->dataHelper->setL10n($l); $this->activityManager->setCurrentUserId($userName); $activityEvents = []; @@ -468,10 +446,27 @@ protected function parseEvent($lang, IEvent $event) { } } + try { + $this->richObjectValidator->validate($event->getRichSubject(), $event->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + $this->logger->logException($e); + $event->setRichSubject('Rich subject or a parameter for "' . $event->getRichSubject() . '" is malformed', []); + $event->setParsedSubject('Rich subject or a parameter for "' . $event->getRichSubject() . '" is malformed'); + } + + if ($event->getRichMessage()) { + try { + $this->richObjectValidator->validate($event->getRichMessage(), $event->getRichMessageParameters()); + } catch (InvalidObjectExeption $e) { + $this->logger->logException($e); + $event->setRichMessage('Rich message or a parameter is malformed', []); + $event->setParsedMessage('Rich message or a parameter is malformed'); + } + } + if (!$event->getParsedSubject()) { - $this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId()); - $event = $this->legacyParser->parse($lang, $event); - $this->activityManager->setFormattingObject('', 0); + $this->logger->debug('Activity "' . $event->getRichSubject() . '" was not parsed by any provider'); + throw new \InvalidArgumentException('Activity "' . $event->getRichSubject() . '" was not parsed by any provider'); } return $event; diff --git a/lib/Parameter/Collection.php b/lib/Parameter/Collection.php deleted file mode 100644 index 3fd6406fb..000000000 --- a/lib/Parameter/Collection.php +++ /dev/null @@ -1,93 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Parameter; - -use OCP\IL10N; - -class Collection implements IParameter { - /** @var IL10N */ - protected $l; - - /** @var Parameter[] */ - protected $parameters; - - /** @var string */ - protected $random; - - /** - * @param IL10N $l - * @param string $random - */ - public function __construct(IL10N $l, $random) { - $this->l = $l; - $this->random = $random; - $this->parameters = []; - } - - /** - * @param IParameter $parameter - */ - public function addParameter(IParameter $parameter) { - foreach ($this->parameters as $existingParameter) { - if ($existingParameter->getParameterInfo() === $parameter->getParameterInfo()) { - return; - } - } - $this->parameters[] = $parameter; - } - - /** - * @return mixed - */ - public function getParameter() { - return $this->random; - } - - /** - * @return array With two entries: value and type - */ - public function getParameterInfo() { - $parameters = []; - foreach ($this->parameters as $parameter) { - $parameters[] = $parameter->getParameterInfo(); - } - - return [ - 'value' => $parameters, - 'type' => 'collection', - ]; - } - - /** - * @return string The formatted parameter - */ - public function format() { - $parameterList = $plainParameterList = []; - - foreach ($this->parameters as $parameter) { - $parameterList[] = $parameter->format(); - } - - return '' . implode('', $parameterList) . ''; - } -} diff --git a/lib/Parameter/Factory.php b/lib/Parameter/Factory.php deleted file mode 100644 index be7d5f897..000000000 --- a/lib/Parameter/Factory.php +++ /dev/null @@ -1,145 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Parameter; - - -use OCA\Activity\CurrentUser; -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Formatter\BaseFormatter; -use OCA\Activity\Formatter\CloudIDFormatter; -use OCA\Activity\Formatter\FileFormatter; -use OCA\Activity\Formatter\UserFormatter; -use OCA\Activity\ViewInfoCache; -use OCP\Activity\IEvent; -use OCP\Activity\IManager; -use OCP\Contacts\IManager as IContactsManager; -use OCP\IL10N; -use OCP\IURLGenerator; -use OCP\IUserManager; - -class Factory { - /** @var IManager */ - protected $activityManager; - - /** @var IUserManager */ - protected $userManager; - - /** @var IContactsManager */ - protected $contactsManager; - - /** @var IL10N */ - protected $l; - - /** @var ViewInfoCache */ - protected $infoCache; - - /** @var string */ - protected $user; - - /** @var IURLGenerator */ - protected $urlGenerator; - - /** - * @param IManager $activityManager - * @param IUserManager $userManager - * @param IURLGenerator $urlGenerator - * @param IContactsManager $contactsManager - * @param ViewInfoCache $infoCache, - * @param IL10N $l - * @param CurrentUser $currentUser - */ - public function __construct(IManager $activityManager, - IUserManager $userManager, - IURLGenerator $urlGenerator, - IContactsManager $contactsManager, - ViewInfoCache $infoCache, - IL10N $l, - CurrentUser $currentUser) { - $this->activityManager = $activityManager; - $this->userManager = $userManager; - $this->urlGenerator = $urlGenerator; - $this->contactsManager = $contactsManager; - $this->infoCache = $infoCache; - $this->l = $l; - $this->user = (string) $currentUser->getUID(); - } - - /** - * @param string $user - */ - public function setUser($user) { - $this->user = (string) $user; - } - - /** - * @param IL10N $l - */ - public function setL10n(IL10N $l) { - $this->l = $l; - } - - /** - * @param string $parameter - * @param IEvent $event - * @param string $formatter - * @return IParameter - */ - public function get($parameter, IEvent $event, $formatter) { - return new Parameter( - $parameter, - $event, - $this->getFormatter($formatter), - $formatter - ); - } - - /** - * @return Collection - */ - public function createCollection() { - return new Collection($this->l, sha1(microtime() . mt_rand())); - } - - /** - * @param string $formatter - * @return IFormatter - */ - protected function getFormatter($formatter) { - switch ($formatter) { - case 'file': - /** @var \OCA\Activity\Formatter\FileFormatter $fileFormatter */ - $fileFormatter = \OC::$server->query(FileFormatter::class); - $fileFormatter->setUser($this->user); - return $fileFormatter; - case 'username': - /** @var \OCA\Activity\Formatter\UserFormatter */ - return \OC::$server->query(UserFormatter::class); - case 'federated_cloud_id': - /** @var \OCA\Activity\Formatter\CloudIDFormatter */ - return \OC::$server->query(CloudIDFormatter::class); - default: - /** @var \OCA\Activity\Formatter\BaseFormatter */ - return \OC::$server->query(BaseFormatter::class); - } - } -} diff --git a/lib/Parameter/IParameter.php b/lib/Parameter/IParameter.php deleted file mode 100644 index 6aaf57979..000000000 --- a/lib/Parameter/IParameter.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Parameter; - - -interface IParameter { - - /** - * A value that is used to check, if the parameter is already in a Collection - * @return mixed - */ - public function getParameter(); - - /** - * @return array With two entries: value and type - */ - public function getParameterInfo(); - - /** - * @return string The formatted parameter - */ - public function format(); -} diff --git a/lib/Parameter/Parameter.php b/lib/Parameter/Parameter.php deleted file mode 100644 index 9c4a22872..000000000 --- a/lib/Parameter/Parameter.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Parameter; - -use OCA\Activity\Formatter\IFormatter; -use OCP\Activity\IEvent; - -class Parameter implements IParameter { - /** @var IFormatter */ - protected $formatter; - - /** @var mixed */ - protected $parameter; - - /** @var IEvent */ - protected $event; - - /** @var string */ - protected $type; - - /** - * @param mixed $parameter - * @param IEvent $event - * @param IFormatter $formatter - * @param string $type - */ - public function __construct($parameter, - IEvent $event, - IFormatter $formatter, - $type) { - $this->parameter = $parameter; - $this->event = $event; - $this->formatter = $formatter; - $this->type = $type; - } - - /** - * @return mixed - */ - public function getParameter() { - if ($this->event->getObjectType() && $this->event->getObjectId()) { - return $this->event->getObjectType() . '#' . $this->event->getObjectId(); - } - - return $this->parameter; - } - - /** - * @return array With two entries: value and type - */ - public function getParameterInfo() { - return [ - 'value' => $this->parameter, - 'type' => $this->type, - ]; - } - - /** - * @return string The formatted parameter - */ - public function format() { - return $this->formatter->format($this->event, $this->parameter); - } -} diff --git a/lib/PlainTextParser.php b/lib/PlainTextParser.php deleted file mode 100644 index f7535999d..000000000 --- a/lib/PlainTextParser.php +++ /dev/null @@ -1,149 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity; - - -use OCP\IL10N; - -class PlainTextParser { - - /** @var IL10N */ - protected $l; - - /** - * @param IL10N $l - */ - public function __construct(IL10N $l) { - $this->l = $l; - } - - /** - * Parse the parameters in the subject and message - * - * @param string $message - * @return string - */ - public function parseMessage($message) { - $message = $this->parseCollections($message); - $message = $this->parseParameters($message); - return $message; - } - - /** - * Parse collections - * - * @param string $message - * @return string - */ - protected function parseCollections($message) { - return preg_replace_callback('/(.*?)<\/collection>/', function($match) { - $parameterList = explode('><', $match[1]); - $parameterListLength = sizeof($parameterList); - - $parameters = []; - for ($i = 0; $i < $parameterListLength; $i++) { - $parameter = $parameterList[$i]; - if ($i > 0) { - $parameter = '<' . $parameter; - } - if ($i + 1 < $parameterListLength) { - $parameter = $parameter . '>'; - } - - $parameters[] = $this->parseParameters($parameter); - } - if ($parameterListLength === 1) { - return array_pop($parameters); - } else { - $lastParameter = array_pop($parameters); - return $this->l->t('%s and %s', [ - implode($this->l->t(', '), $parameters), - $lastParameter, - ]); - } - }, $message); - } - - /** - * Parse the parameters in the subject and message - * - * @param string $message - * @return string - */ - protected function parseParameters($message) { - $message = $this->parseUntypedParameters($message); - $message = $this->parseUserParameters($message); - $message = $this->parseFederatedCloudIDParameters($message); - $message = $this->parseFileParameters($message); - return $message; - } - - /** - * Display the parameter value - * - * @param string $message - * @return string - */ - protected function parseUntypedParameters($message) { - return preg_replace_callback('/(.*?)<\/parameter>/', function($match) { - return $match[1]; - }, $message); - } - - /** - * Display the users display name - * - * @param string $message - * @return string - */ - protected function parseUserParameters($message) { - return preg_replace_callback('/(.*?)<\/user>/', function($match) { - // We don't want HTML to work, but quote signs are okay. - return str_replace('"', '"', $match[1]); - }, $message); - } - - /** - * Display the full cloud id - * - * @param string $message - * @return string - */ - protected function parseFederatedCloudIDParameters($message) { - return preg_replace_callback('/(.*?)<\/federated-cloud-id>/', function($match) { - return $match[1]; - }, $message); - } - - /** - * Display the path for files - * - * @param string $message - * @return string - */ - protected function parseFileParameters($message) { - return preg_replace_callback('/(.*?)<\/file>/', function($match) { - return $match[3]; - }, $message); - } -} diff --git a/tests/AppInfo/ApplicationTest.php b/tests/AppInfo/ApplicationTest.php index 666ab206e..983e0010e 100644 --- a/tests/AppInfo/ApplicationTest.php +++ b/tests/AppInfo/ApplicationTest.php @@ -36,20 +36,12 @@ use OCA\Activity\Controller\Settings; use OCA\Activity\CurrentUser; use OCA\Activity\Data; -use OCA\Activity\DataHelper; use OCA\Activity\FilesHooks; -use OCA\Activity\Formatter\BaseFormatter; -use OCA\Activity\Formatter\CloudIDFormatter; -use OCA\Activity\Formatter\FileFormatter; -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Formatter\UserFormatter; use OCA\Activity\GroupHelper; use OCA\Activity\GroupHelperDisabled; use OCA\Activity\Hooks; use OCA\Activity\MailQueueHandler; use OCA\Activity\Navigation; -use OCA\Activity\Parameter\Factory; -use OCA\Activity\PlainTextParser; use OCA\Activity\Tests\TestCase; use OCA\Activity\UserSettings; use OCA\Activity\ViewInfoCache; @@ -95,7 +87,6 @@ public function queryData() { array(Consumer::class, IConsumer::class), array(CurrentUser::class), array(Data::class), - array(DataHelper::class), array(FilesHooks::class), array(GroupHelper::class), array(GroupHelperDisabled::class), @@ -103,7 +94,6 @@ public function queryData() { array(Hooks::class), array(MailQueueHandler::class), array(Navigation::class), - array(PlainTextParser::class), array(UserSettings::class), array(ViewInfoCache::class), @@ -126,23 +116,6 @@ public function queryData() { array('FeedController', Controller::class), array('SettingsController', Settings::class), array('SettingsController', Controller::class), - - // Formatter - array(BaseFormatter::class), - array(BaseFormatter::class, IFormatter::class), - array(CloudIDFormatter::class), - array(CloudIDFormatter::class, IFormatter::class), - array(FileFormatter::class), - array(FileFormatter::class, IFormatter::class), - array(UserFormatter::class), - array(UserFormatter::class, IFormatter::class), - - // Parameter - array(Factory::class), - //array(Collection::class), - //array(Collection::class, IParameter::class), - //array(Parameter::class), - //array(Parameter::class, IParameter::class), ); } @@ -155,6 +128,6 @@ public function testContainerQuery($service, $expected = null) { if ($expected === null) { $expected = $service; } - $this->assertTrue($this->container->query($service) instanceof $expected); + $this->assertInstanceOf($expected, $this->container->query($service)); } } diff --git a/tests/Controller/APIv1Test.php b/tests/Controller/APIv1Test.php index 9f901f50a..1d813c6b6 100644 --- a/tests/Controller/APIv1Test.php +++ b/tests/Controller/APIv1Test.php @@ -25,12 +25,10 @@ use OCA\Activity\Controller\APIv1; use OCA\Activity\CurrentUser; use OCA\Activity\Data; -use OCA\Activity\DataHelper; -use OCA\Activity\Extension\LegacyParser; use OCA\Activity\GroupHelper; -use OCA\Activity\Parameter\Factory; -use OCA\Activity\PlainTextParser; -use OCA\Activity\Tests\Mock\Extension; +use OCA\Activity\Tests\Mock\Provider; +use OCA\Activity\Tests\Mock\Setting1; +use OCA\Activity\Tests\Mock\Setting2; use OCA\Activity\Tests\TestCase; use OCA\Activity\UserSettings; use OCP\Activity\IExtension; @@ -39,9 +37,7 @@ use OCP\IL10N; use OCP\ILogger; use OCP\IRequest; -use OCP\IURLGenerator; use OCP\IUserSession; -use OCP\L10N\IFactory; use OCP\RichObjectStrings\IValidator; /** @@ -108,8 +104,7 @@ protected function tearDown() { protected function cleanUp() { $data = new Data( $this->getMockBuilder(IManager::class)->getMock(), - \OC::$server->getDatabaseConnection(), - $this->getMockBuilder(IUserSession::class)->getMock() + \OC::$server->getDatabaseConnection() ); $this->deleteUser($data, 'activity-api-user1'); @@ -140,7 +135,7 @@ public function getData() { 'date' => null, 'id' => null, 'message' => '', - 'subject' => 'Subject2 @User #A/B.txt', + 'subject' => 'Subject2 @User #/A/B.txt', ), array( 'link' => 'link', @@ -148,7 +143,7 @@ public function getData() { 'date' => null, 'id' => null, 'message' => '', - 'subject' => 'Subject1 #A/B.txt', + 'subject' => 'Subject1 #/A/B.txt', ), )), array('activity-api-user1', 0, 1, array( @@ -158,7 +153,7 @@ public function getData() { 'date' => null, 'id' => null, 'message' => '', - 'subject' => 'Subject2 @User #A/B.txt', + 'subject' => 'Subject2 @User #/A/B.txt', ), )), array('activity-api-user1', 1, 1, array( @@ -168,7 +163,7 @@ public function getData() { 'date' => null, 'id' => null, 'message' => '', - 'subject' => 'Subject1 #A/B.txt', + 'subject' => 'Subject1 #/A/B.txt', ), )), array('activity-api-user1', 5, 1, array( @@ -178,7 +173,7 @@ public function getData() { 'date' => null, 'id' => null, 'message' => '', - 'subject' => 'Subject2 @User #A/B.txt', + 'subject' => 'Subject2 @User #/A/B.txt', ), )), ); @@ -204,11 +199,6 @@ public function testGet($user, $start, $count, $expected) { ->will($this->returnCallback(function($text, $parameters = array()) { return vsprintf($text, $parameters); })); - $languageFactory = $this->createMock(IFactory::class); - $languageFactory->expects(empty($expected) ? $this->never() : $this->atLeastOnce()) - ->method('get') - ->with('activity') - ->willReturn($l); $activityManager = new Manager( $this->getMockBuilder(IRequest::class)->getMock(), @@ -216,9 +206,9 @@ public function testGet($user, $start, $count, $expected) { $config, \OC::$server->query(IValidator::class) ); - $activityManager->registerExtension(function() use ($l) { - return new Extension($l, $this->getMockBuilder(IURLGenerator::class)->getMock()); - }); + $activityManager->registerProvider(Provider::class); + $activityManager->registerSetting(Setting1::class); + $activityManager->registerSetting(Setting2::class); $currentUser = $this->getMockBuilder(CurrentUser::class) ->disableOriginalConstructor() @@ -228,22 +218,14 @@ public function testGet($user, $start, $count, $expected) { ->willReturn($user); $data = new Data($activityManager, \OC::$server->getDatabaseConnection()); - $dataHelper = new DataHelper( - $activityManager, - \OC::$server->query(Factory::class), - $this->getMockBuilder(IFactory::class)->getMock(), - $l - ); - $parser = new PlainTextParser($l); /** @var APIv1 $controller */ $controller = new APIv1( 'activity', $this->getMockBuilder(IRequest::class)->getMock(), $data, - new GroupHelper($l, $activityManager, $dataHelper, $this->createMock(IValidator::class), $this->createMock(ILogger::class), new LegacyParser($languageFactory, $dataHelper, $parser)), - new UserSettings($activityManager, $config, $data), - new PlainTextParser($l), + new GroupHelper($l, $activityManager, $this->createMock(IValidator::class), $this->createMock(ILogger::class)), + new UserSettings($activityManager, $config), $currentUser ); $response = $controller->get($start, $count); diff --git a/tests/DataHelperTest.php b/tests/DataHelperTest.php deleted file mode 100644 index 010d03832..000000000 --- a/tests/DataHelperTest.php +++ /dev/null @@ -1,394 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests; - -use OCA\Activity\DataHelper; - -class DataHelperTest extends TestCase { - protected $originalWEBROOT; - /** @var \OCP\Activity\IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $activityManager; - /** @var \OCA\Activity\Parameter\Factory|\PHPUnit_Framework_MockObject_MockObject */ - protected $parameterFactory; - /** @var \OCP\L10N\IFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $l10Nfactory; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->originalWEBROOT = \OC::$WEBROOT; - \OC::$WEBROOT = ''; - - $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->parameterFactory = $this->getMockBuilder('OCA\Activity\Parameter\Factory') - ->disableOriginalConstructor() - ->getMock(); - - $this->l10Nfactory = $this->getMockBuilder('OCP\L10N\IFactory') - ->disableOriginalConstructor() - ->getMock(); - - $this->l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function tearDown() { - \OC::$WEBROOT = $this->originalWEBROOT; - parent::tearDown(); - } - - /** - * @param array $methods - * @return DataHelper|\PHPUnit_Framework_MockObject_MockObject - */ - protected function getHelper(array $methods = []) { - if (empty($methods)) { - return new DataHelper( - $this->activityManager, - $this->parameterFactory, - $this->l10Nfactory, - $this->l - ); - } else { - return $this->getMockBuilder('OCA\Activity\DataHelper') - ->setConstructorArgs([ - $this->activityManager, - $this->parameterFactory, - $this->l10Nfactory, - $this->l, - ]) - ->setMethods($methods) - ->getMock(); - } - } - - protected function getParameter($return) { - $parameter = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $parameter->expects($this->once()) - ->method('format') - ->willReturn($return); - return $parameter; - } - - public function dataTranslation() { - return [ - ['app1', 'text1', [], [], false, 'lang'], - ['app2', 'text2', [], [], false, 'lang'], - ['app2', 'text2', [], [], 'manager', 'manager'], - - ['app2', 'text2', [$this->getParameter('return1')], ['return1'], 'manager', 'manager'], - ['app2', 'text2', [$this->getParameter('return2')], ['return2'], false, 'lang'], - ]; - } - - /** - * @dataProvider dataTranslation - * - * @param string $app - * @param string $text - * @param array $params - * @param array $prepared - * @param bool|string $managerReturn - * @param string $expected - */ - public function testTranslation($app, $text, array $params, array $prepared, $managerReturn, $expected) { - - $this->activityManager->expects($this->once()) - ->method('translate') - ->with($app, $text, $prepared, false, false) - ->willReturn($managerReturn); - if ($managerReturn === false) { - $l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - $l->expects($this->once()) - ->method('t') - ->with($text, $prepared) - ->willReturn('lang'); - $this->l10Nfactory->expects($this->once()) - ->method('get') - ->with($app, $this->anything()) - ->willReturn($l); - } - - $helper = $this->getHelper(); - $this->assertSame( - $expected, - (string) $helper->translation($app, $text, $params) - ); - } - - public function testTranslationNoText() { - $this->activityManager->expects($this->never()) - ->method('translate'); - - $helper = $this->getHelper(); - $this->assertSame('', $helper->translation('', '', [])); - } - - public function dataGetSpecialParameterList() { - return [ - ['app1', 'text1', false, []], - ['app2', 'text2', [], []], - ['app3', 'text3', [0 => 'username'], [0 => 'username']], - ]; - } - - /** - * @dataProvider dataGetSpecialParameterList - * - * @param string $app - * @param string $text - * @param array|bool $managerReturn - * @param array $expected - */ - public function testGetSpecialParameterList($app, $text, $managerReturn, array $expected) { - $this->activityManager->expects($this->once()) - ->method('getSpecialParameterList') - ->with($app, $text) - ->willReturn($managerReturn); - - $instance = $this->getHelper(); - $this->assertSame($expected, $this->invokePrivate($instance, 'getSpecialParameterList', [$app, $text])); - } - - public function dataFormatString() { - return [ - [ - [ - 'app' => 'app1', - 'subject' => 'subject1', - 'subjectparams_array' => [], - 'message' => 'message1', - 'messageparams_array' => [], - ], - 'subject', - [ - 'app' => 'app1', - 'subject' => 'subject1', - 'message' => 'message1', - 'messageparams_array' => [], - 'subjectparams' => [], - 'subject_prepared' => 'translation', - ], - ], - [ - [ - 'app' => 'app1', - 'subject' => 'subject1', - 'subjectparams_array' => [], - 'message' => 'message1', - 'messageparams_array' => [], - ], - 'message', - [ - 'app' => 'app1', - 'subject' => 'subject1', - 'subjectparams_array' => [], - 'message' => 'message1', - 'messageparams' => [], - 'message_prepared' => 'translation', - ], - ], - ]; - } - - /** - * @dataProvider dataFormatString - * - * @param array $activity - * @param string $message - * @param array $expected - */ - public function testFormatString(array $activity, $message, array $expected) { - $instance = $this->getHelper(['translation']); - - $instance->expects($this->once()) - ->method('translation') - ->with($activity['app'], $activity[$message], $activity[$message . 'params_array']) - ->willReturnCallback(function() { - return 'translation'; - }); - - $this->assertSame($expected, $instance->formatStrings($activity, $message)); - } - - public function dataGetParameters() { - return [ - ['subject', 'params1', [], [], [], []], - [ - 'subject', - 'params1', - ['one', 'two'], - [], - [['one', 'base'], ['two', 'base']], - ['param1', 'param2'], - ], - [ - 'subject', - 'params1', - ['one', 'two'], - ['user'], - [['one', 'user'], ['two', 'base']], - ['param1', 'param2'], - ], - [ - 'message', - 'params1', - ['one', 'two'], - [1 => 'user'], - [['one', 'base'], ['two', 'user']], - ['param1', 'param2'], - ], - ]; - } - - /** - * @dataProvider dataGetParameters - * - * @param string $parsing - * @param string $parameterString - * @param array $parameters - * @param array $parameterTypes - * @param array $factoryCalls - * @param array $expected - */ - public function testGetParameters($parsing, $parameterString, array $parameters, array $parameterTypes, array $factoryCalls, array $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - $event->expects($this->once()) - ->method('getApp') - ->willReturn('app'); - - if ($parsing === 'subject') { - $event->expects($this->once()) - ->method('getSubject') - ->willReturn('text'); - $event->expects($this->never()) - ->method('getMessage'); - } else { - $event->expects($this->never()) - ->method('getSubject'); - $event->expects($this->once()) - ->method('getMessage') - ->willReturn('text'); - } - - global $call; - $call = 0; - foreach ($factoryCalls as $i => $factoryCall) { - $this->parameterFactory->expects($this->at($i)) - ->method('get') - ->with($factoryCall[0], $event, $factoryCall[1]) - ->willReturnCallback(function() { - global $call; - $call++; - return 'param' . $call; - }); - } - - $instance = $this->getHelper([ - 'parseParameters', - 'getSpecialParameterList', - ]); - - $instance->expects($this->once()) - ->method('parseParameters') - ->with($parameterString) - ->willReturn($parameters); - $instance->expects($this->once()) - ->method('getSpecialParameterList') - ->with('app', 'text') - ->willReturn($parameterTypes); - - $this->assertSame($expected, $instance->getParameters($event, $parsing, $parameterString)); - } - - public function testSetUser() { - $helper = $this->getHelper(); - $this->parameterFactory->expects($this->once()) - ->method('setUser') - ->with('foobar'); - - $helper->setUser('foobar'); - } - - public function testSetL10n() { - $l = \OC::$server->getL10NFactory()->get('activity', 'de'); - - $helper = $this->getHelper(); - $this->parameterFactory->expects($this->once()) - ->method('setL10n') - ->with($l); - - $helper->setL10n($l); - } - - public function dataParseParameters() { - return [ - [false, []], - ['a', ['a']], - ['"foo"bar"', ['"foo"bar"']], - [serialize('a'), ['a']], - [serialize(['a']), ['a']], - [json_encode(['a']), ['a']], - ]; - } - - /** - * @dataProvider dataParseParameters - * @param string $stringInput - * @param array $expected - */ - public function testParseParameters($stringInput, $expected) { - $helper = $this->getHelper(); - $this->assertEquals($expected, $this->invokePrivate($helper, 'parseParameters', [$stringInput])); - } - - public function testCreateCollection() { - $helper = $this->getHelper(); - - $collection = $this->getMockBuilder('OCA\Activity\Parameter\Collection') - ->disableOriginalConstructor() - ->getMock(); - - $this->parameterFactory->expects($this->once()) - ->method('createCollection') - ->willReturn($collection); - - $return = $this->invokePrivate($helper, 'createCollection'); - $this->assertEquals($collection, $return); - $this->assertInstanceOf('OCA\Activity\Parameter\Collection', $return); - $this->assertInstanceOf('OCA\Activity\Parameter\IParameter', $return); - } -} diff --git a/tests/Formatter/BaseFormatterTest.php b/tests/Formatter/BaseFormatterTest.php deleted file mode 100644 index d8bfaa425..000000000 --- a/tests/Formatter/BaseFormatterTest.php +++ /dev/null @@ -1,69 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Formatter; - -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Formatter\BaseFormatter; -use OCA\Activity\Tests\TestCase; - -class BaseFormatterTest extends TestCase { - - /** - * @param array $methods - * @return IFormatter|\PHPUnit_Framework_MockObject_MockObject - */ - public function getFormatter(array $methods = []) { - if (empty($methods)) { - return new BaseFormatter(); - } else { - return $this->getMockBuilder('OCA\Activity\Formatter\BaseFormatter') - ->setConstructorArgs([ - ]) - ->setMethods($methods) - ->getMock(); - } - } - - public function dataFormat() { - return [ - ['paraeter1', 'para<m>eter1'], - ['paraeter2', 'para<m>eter2'], - ]; - } - - /** - * @dataProvider dataFormat - * - * @param string $parameter - * @param string $expected - */ - public function testFormat($parameter, $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $formatter = $this->getFormatter(); - $this->assertSame($expected, $formatter->format($event, $parameter)); - } -} diff --git a/tests/Formatter/CloudIDFormatterTest.php b/tests/Formatter/CloudIDFormatterTest.php deleted file mode 100644 index ca77fcba6..000000000 --- a/tests/Formatter/CloudIDFormatterTest.php +++ /dev/null @@ -1,189 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Formatter; - -use OCA\Activity\Formatter\CloudIDFormatter; -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Tests\TestCase; - -class CloudIDFormatterTest extends TestCase { - /** @var \OCP\Contacts\IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $contactsManager; - - protected function setUp() { - parent::setUp(); - - $this->contactsManager = $this->getMockBuilder('OCP\Contacts\IManager') - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * @param array $methods - * @return IFormatter|\PHPUnit_Framework_MockObject_MockObject - */ - public function getFormatter(array $methods = []) { - if (empty($methods)) { - return new CloudIDFormatter( - $this->contactsManager - ); - } else { - return $this->getMockBuilder('OCA\Activity\Formatter\CloudIDFormatter') - ->setConstructorArgs([ - $this->contactsManager, - ]) - ->setMethods($methods) - ->getMock(); - } - } - - public function dataFormat() { - return [ - ['test1', 'test1'], - ['test1@localhost1', 'test1@localhost1'], - ['test2@localhost2', 'test2@localhost2'], - ['tst@lcalhost', 't<e>st@l<o>calhost'], - - ]; - } - - /** - * @dataProvider dataFormat - * - * @param string $parameter - * @param string $expected - */ - public function testFormat($parameter, $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $formatter = $this->getFormatter(['getDisplayNameFromContact']); - $formatter->expects($this->once()) - ->method('getDisplayNameFromContact') - ->willThrowException(new \OutOfBoundsException()); - - $this->assertSame($expected, $formatter->format($event, $parameter)); - } - - public function dataGetDisplayNameFromContact() { - return [ - [ - 'test@localhost', - [ - [ - 'FN' => 'Test On Localhost', - 'CLOUD' => [ - 'test@localhost' - ], - ], - ], - 'Test On Localhost', - ], - [ - 'test@localhost', - [ - [ - 'FN' => 'Test On Localhost', - 'CLOUD' => [ - 'test1@localhost', - 'test@localhost', - ], - ], - ], - 'Test On Localhost', - ], - ]; - } - - /** - * @dataProvider dataGetDisplayNameFromContact - * - * @param string $cloudId - * @param array $addressReturn - * @param string $expected - */ - public function testGetDisplayNameFromContact($cloudId, array $addressReturn, $expected) { - $this->contactsManager->expects($this->once()) - ->method('search') - ->with($cloudId, ['CLOUD']) - ->willReturn($addressReturn); - - $formatter = $this->getFormatter(); - $this->assertSame($expected, $this->invokePrivate($formatter, 'getDisplayNameFromContact', [$cloudId])); - $this->assertSame($expected, $this->invokePrivate($formatter, 'getDisplayNameFromContact', [$cloudId])); - } - - - public function dataGetDisplayNameFromContactThrows() { - return [ - [ - [], - ], - [ - [ - [ - 'FN' => 'test@localhost', - ], - ], - ], - [ - [ - [ - 'FN' => 'test@localhost', - 'CLOUD' => [], - ], - ], - ], - ]; - } - - /** - * @dataProvider dataGetDisplayNameFromContactThrows - * - * @param array $addressReturn - */ - public function testGetDisplayNameFromContactThrows(array $addressReturn) { - $this->contactsManager->expects($this->once()) - ->method('search') - ->with('test@localhost', ['CLOUD']) - ->willReturn($addressReturn); - - $formatter = $this->getFormatter(); - - try { - $this->invokePrivate($formatter, 'getDisplayNameFromContact', ['test@localhost']); - $this->assertTrue(false, 'Asserting that OutOfBoundsException is thrown'); - } catch (\OutOfBoundsException $e) { - $this->assertTrue(true, 'Asserting that OutOfBoundsException is thrown'); - } - - try { - $this->invokePrivate($formatter, 'getDisplayNameFromContact', ['test@localhost']); - $this->assertTrue(false, 'Asserting that OutOfBoundsException is thrown'); - } catch (\OutOfBoundsException $e) { - $this->assertTrue(true, 'Asserting that OutOfBoundsException is thrown'); - } - } -} diff --git a/tests/Formatter/FileFormatterTest.php b/tests/Formatter/FileFormatterTest.php deleted file mode 100644 index 5f7f841f7..000000000 --- a/tests/Formatter/FileFormatterTest.php +++ /dev/null @@ -1,223 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Formatter; - -use OCA\Activity\Formatter\FileFormatter; -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Tests\TestCase; - -class FileFormatterTest extends TestCase { - /** @var \OCP\IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlGenerator; - - /** @var \OCA\Activity\ViewInfoCache|\PHPUnit_Framework_MockObject_MockObject */ - protected $infoCache; - - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator') - ->disableOriginalConstructor() - ->getMock(); - - $this->infoCache = $this->getMockBuilder('OCA\Activity\ViewInfoCache') - ->disableOriginalConstructor() - ->getMock(); - - $this->l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - $this->l->expects($this->any()) - ->method('t') - ->willReturnCallback(function ($string, $parameters) { - return vsprintf($string, $parameters); - }); - } - - /** - * @param array $methods - * @param string $user - * @return IFormatter|\PHPUnit_Framework_MockObject_MockObject - */ - public function getFormatter(array $methods = [], $user = 'user') { - if (empty($methods)) { - $formatter = new FileFormatter( - $this->infoCache, - $this->urlGenerator, - $this->l - ); - $formatter->setUser($user); - return $formatter; - } else { - $formatter = $this->getMockBuilder('OCA\Activity\Formatter\FileFormatter') - ->setConstructorArgs([ - $this->infoCache, - $this->urlGenerator, - $this->l, - $user, - ]) - ->setMethods($methods) - ->getMock(); - $this->invokePrivate($formatter, 'user', [$user]); - return $formatter; - } - } - - public function dataFormat() { - $trash0 = [ - 'path' => '/test2', - 'is_dir' => false, - 'exists' => true, - 'view' => 'trashbin', - ]; - $trash1 = [ - 'path' => '/test2', - 'is_dir' => true, - 'exists' => true, - 'view' => 'trashbin', - ]; - - return [ - ['user1', '/test1', false, [], 'test1'], - ['user1', '/test1', true, [], 'test1'], - ['user1', '/', true, [], '/'], - ['user1', '/test1/test2', false, [], 'test1/test2'], - ['user1', '/test1/test2', true, [], 'test1/test2'], - - ['user1', '/test1/test2', false, $trash0, 'test1/test2'], - ['user1', '/test1/test2', true, $trash1, 'test1/test2'], - - ['user2', '/test1', false, [], 'test1'], - ]; - } - - /** - * @dataProvider dataFormat - * - * @param string $user - * @param string $parameter - * @param bool $isDir - * @param array $info - * @param string $expected - */ - public function testFormat($user, $parameter, $isDir, array $info, $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - if (!empty($info)) { - $event->expects($this->once()) - ->method('getObjectType') - ->willReturn('files'); - $event->expects($this->once()) - ->method('getObjectName') - ->willReturn($parameter); - $event->expects($this->once()) - ->method('getObjectId') - ->willReturn(42); - } - - $this->urlGenerator->expects($this->once()) - ->method('linkToRouteAbsolute') - ->with('files.view.index', $this->anything()) - ->willReturnCallback(function($route, $parameters) { - $paramList = []; - foreach ($parameters as $key => $value) { - $paramList[] = $key . '=' . urlencode($value); - } - return 'apps/files/' . '?' . implode('&', $paramList); - }); - - $formatter = $this->getFormatter([ - 'fixLegacyFilename', - ], $user); - $formatter->expects($this->once()) - ->method('fixLegacyFilename') - ->willReturnArgument(0); - if (!empty($info)) { - $this->infoCache->expects($this->once()) - ->method('getInfoById') - ->with($user, 42, $parameter) - ->willReturn($info); - } else { - $this->infoCache->expects($this->once()) - ->method('getInfoByPath') - ->with($user, $parameter) - ->willReturn([ - 'path' => $parameter, - 'is_dir' => $isDir, - 'exists' => true, - 'view' => '', - ]); - } - - $this->assertSame($expected, $formatter->format($event, $parameter)); - } - - public function dataFixLegacyFilename() { - return [ - ['test1', '/test1'], - ['/test1', '/test1'], - ['test1/test2', '/test1/test2'], - ['/test1/test2', '/test1/test2'], - [[42 => 'fourtytwo'], [42 => 'fourtytwo']], - [[23 => '/twentythree'], [23 => '/twentythree']], - ]; - } - - /** - * @dataProvider dataFixLegacyFilename - * - * @param string $filename - * @param string $expected - */ - public function testFixLegacyFilename($filename, $expected) { - $formatter = $this->getFormatter(); - $this->assertSame($expected, $this->invokePrivate($formatter, 'fixLegacyFilename', [$filename])); - } - - public function dataSplitPathFromFilename() { - return [ - ['test1', ['', 'test1']], - ['/test1', ['', 'test1']], - ['test1/test2', ['test1', 'test2']], - ['/test1/test2', ['test1', 'test2']], - ['test1/test2/test3', ['test1/test2', 'test3']], - ['/test1/test2/test3', ['test1/test2', 'test3']], - ]; - } - - /** - * @dataProvider dataSplitPathFromFilename - * - * @param string $filename - * @param string $expected - */ - public function testSplitPathFromFilename($filename, $expected) { - $formatter = $this->getFormatter(); - $this->assertSame($expected, $this->invokePrivate($formatter, 'splitPathFromFilename', [$filename])); - } -} diff --git a/tests/Formatter/UserFormatterTest.php b/tests/Formatter/UserFormatterTest.php deleted file mode 100644 index 8df625b15..000000000 --- a/tests/Formatter/UserFormatterTest.php +++ /dev/null @@ -1,182 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Formatter; - -use OCA\Activity\Formatter\IFormatter; -use OCA\Activity\Formatter\UserFormatter; -use OCA\Activity\Tests\TestCase; - -class UserFormatterTest extends TestCase { - - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - /** @var \OCA\Activity\Formatter\CloudIDFormatter|\PHPUnit_Framework_MockObject_MockObject */ - protected $cloudIdFormatter; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->userManager = $this->getMockBuilder('OCP\IUserManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->cloudIdFormatter = $this->getMockBuilder('OCA\Activity\Formatter\CloudIDFormatter') - ->disableOriginalConstructor() - ->getMock(); - - $this->l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * @param array $methods - * @return IFormatter|\PHPUnit_Framework_MockObject_MockObject - */ - public function getFormatter(array $methods = []) { - if (empty($methods)) { - return new UserFormatter( - $this->userManager, - $this->cloudIdFormatter, - $this->l - ); - } else { - return $this->getMockBuilder('OCA\Activity\Formatter\UserFormatter') - ->setConstructorArgs([ - $this->userManager, - $this->cloudIdFormatter, - $this->l, - ]) - ->setMethods($methods) - ->getMock(); - } - } - - public function dataFormatRemoteUser() { - return [ - [''], - ]; - } - - /** - * @dataProvider dataFormatRemoteUser - * - * @param string $expected - */ - public function testFormatRemoteUser($expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $this->l->expects($this->once()) - ->method('t') - ->willReturnCallback(function ($string, $parameters) { - return vsprintf($string, $parameters); - }); - $this->userManager->expects($this->never()) - ->method('get'); - - $formatter = $this->getFormatter(); - $this->assertSame($expected, $formatter->format($event, '')); - } - - protected function getUser($displayName) { - $user = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn($displayName); - return $user; - } - - public function dataFormat() { - return [ - ['nouser', null, 'nouser'], - ['user1', $this->getUser('Display1'), 'user1'], - ['user5', $this->getUser('Display5'), 'user5'], - ]; - } - - /** - * @dataProvider dataFormat - * - * @param string $parameter - * @param null|\OCP\IUser $user - * @param string $expected - */ - public function testFormat($parameter, $user, $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $this->l->expects($this->never()) - ->method('t'); - $this->userManager->expects($this->once()) - ->method('get') - ->with($parameter) - ->willReturn($user); - - $formatter = $this->getFormatter(); - $this->assertSame($expected, $formatter->format($event, $parameter)); - } - - public function dataFormatCloudUser() { - return [ - ['user@localhost', true, 'user@localhost'], - ['user-no at-localhost', false, 'user-no at-localhost'], - ]; - } - - /** - * @dataProvider dataFormatCloudUser - * - * @param string $parameter - * @param bool $cloudFormatter - * @param string $expected - */ - public function testFormatCloudUser($parameter, $cloudFormatter, $expected) { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $this->l->expects($this->never()) - ->method('t'); - $this->userManager->expects($this->once()) - ->method('get') - ->with($parameter) - ->willReturn(null); - $this->cloudIdFormatter->expects($cloudFormatter ? $this->once() : $this->never()) - ->method('format') - ->with($event, $parameter) - ->willReturn($expected); - - $formatter = $this->getFormatter(); - $this->assertSame($expected, $formatter->format($event, $parameter)); - } -} diff --git a/tests/GroupHelperTest.php b/tests/GroupHelperTest.php index 7fa93e476..2d0e63d36 100644 --- a/tests/GroupHelperTest.php +++ b/tests/GroupHelperTest.php @@ -22,11 +22,8 @@ namespace OCA\Activity\Tests; -use OCA\Activity\DataHelper; -use OCA\Activity\Extension\LegacyParser; use OCA\Activity\GroupHelper; use OCA\Activity\GroupHelperDisabled; -use OCA\Activity\Parameter\Collection; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\IL10N; @@ -36,14 +33,10 @@ class GroupHelperTest extends TestCase { /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ protected $activityManager; - /** @var DataHelper|\PHPUnit_Framework_MockObject_MockObject */ - protected $dataHelper; /** @var IValidator|\PHPUnit_Framework_MockObject_MockObject */ protected $validator; /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ protected $logger; - /** @var LegacyParser|\PHPUnit_Framework_MockObject_MockObject */ - protected $legacyParser; /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ protected $l; @@ -52,8 +45,6 @@ protected function setUp() { $this->l = $this->createMock(IL10N::class); $this->activityManager = $this->createMock(IManager::class); - $this->dataHelper = $this->createMock(DataHelper::class); - $this->legacyParser = $this->createMock(LegacyParser::class); $this->validator = $this->createMock(IValidator::class); $this->logger = $this->createMock(ILogger::class); } @@ -69,48 +60,33 @@ protected function getHelper(array $methods = [], $grouping = false) { return new GroupHelper( $this->l, $this->activityManager, - $this->dataHelper, $this->validator, - $this->logger, - $this->legacyParser + $this->logger ); } return new GroupHelperDisabled( $this->l, $this->activityManager, - $this->dataHelper, $this->validator, - $this->logger, - $this->legacyParser + $this->logger ); } - return $this->getMockBuilder($grouping ? 'OCA\Activity\GroupHelper' : 'OCA\Activity\GroupHelperDisabled') + return $this->getMockBuilder($grouping ? GroupHelper::class : GroupHelperDisabled::class) ->setConstructorArgs([ + $this->l, $this->activityManager, - $this->dataHelper + $this->validator, + $this->logger ]) ->setMethods($methods) ->getMock(); } - public function testSetUser() { - $helper = $this->getHelper(); - - $this->dataHelper->expects($this->once()) - ->method('setUser') - ->with('foobar'); - - $helper->setUser('foobar'); - } - public function testSetL10n() { $helper = $this->getHelper(); $l = \OC::$server->getL10NFactory()->get('activity', 'de'); - $this->dataHelper->expects($this->once()) - ->method('setL10n') - ->with($l); $helper->setL10n($l); } diff --git a/tests/MailQueueHandlerTest.php b/tests/MailQueueHandlerTest.php index 98f4f1333..4d033142b 100644 --- a/tests/MailQueueHandlerTest.php +++ b/tests/MailQueueHandlerTest.php @@ -23,7 +23,6 @@ namespace OCA\Activity\Tests; -use OCA\Activity\Extension\LegacyParser; use OCA\Activity\MailQueueHandler; use OCP\IConfig; use OCP\IL10N; @@ -35,10 +34,11 @@ use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OC\Mail\Message; -use OCA\Activity\DataHelper; use OCP\IURLGenerator; use OCP\IDateTimeFormatter; use OCP\IUser; +use OCP\RichObjectStrings\IValidator; +use PHPUnit\Framework\MockObject\MockObject; /** * Class MailQueueHandlerTest @@ -68,11 +68,8 @@ class MailQueueHandlerTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject|IManager */ protected $activityManager; - /** @var \PHPUnit_Framework_MockObject_MockObject|DataHelper */ - protected $dataHelper; - - /** @var \PHPUnit_Framework_MockObject_MockObject|LegacyParser */ - protected $legacyParser; + /** @var IValidator|MockObject */ + protected $richObjectValidator; /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ protected $config; @@ -89,7 +86,6 @@ protected function setUp() { $app = self::getUniqueID('MailQueueHandlerTest'); $this->userManager = $this->createMock(IUserManager::class); $this->lFactory = $this->createMock(IFactory::class); - $this->legacyParser = $this->createMock(LegacyParser::class); $this->config = $this->createMock(IConfig::class); $this->logger = $this->createMock(ILogger::class); $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); @@ -143,16 +139,8 @@ protected function setUp() { ->method('getProviders') ->willReturn([]); - $this->legacyParser->expects($this->any()) - ->method('parse') - ->willReturnArgument(1); - - $this->dataHelper = $this->createMock(DataHelper::class); - $this->dataHelper->expects($this->any()) - ->method('getParameters') - ->willReturn([]); - $this->message = $this->createMock(Message::class); + $this->richObjectValidator = $this->createMock(IValidator::class); $this->mailer = $this->createMock(IMailer::class); $this->mailer->expects($this->any()) ->method('createMessage') @@ -160,13 +148,12 @@ protected function setUp() { $this->mailQueueHandler = new MailQueueHandler( $this->dateTimeFormatter, $connection, - $this->dataHelper, $this->mailer, $this->createMock(IURLGenerator::class), $this->userManager, $this->lFactory, $this->activityManager, - $this->legacyParser, + $this->richObjectValidator, $this->config, $this->logger ); diff --git a/tests/Parameter/CollectionTest.php b/tests/Parameter/CollectionTest.php deleted file mode 100644 index fdafc510e..000000000 --- a/tests/Parameter/CollectionTest.php +++ /dev/null @@ -1,187 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Parameter; - -use OCA\Activity\Parameter\Collection; -use OCA\Activity\Tests\TestCase; - -class CollectionTest extends TestCase { - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * @param array $methods - * @param string $random - * @return Collection|\PHPUnit_Framework_MockObject_MockObject - */ - public function getCollection(array $methods = [], $random = 'random') { - if (empty($methods)) { - return new Collection( - $this->l, - $random - ); - } else { - return $this->getMockBuilder('OCA\Activity\Parameter\Collection') - ->setConstructorArgs([ - $this->l, - $random, - ]) - ->setMethods($methods) - ->getMock(); - } - } - - public function dataGetParameter() { - return [ - ['random1'], - ['random2'], - ]; - } - - /** - * @dataProvider dataGetParameter - * @param string $random - */ - public function testGetParameter($random) { - $collection = $this->getCollection([], $random); - $this->assertSame($random, $collection->getParameter()); - } - - public function dataGetParameterInfo() { - return [ - [ - [], - [], - ], - [ - [ - [ - 'value' => 'value1', - 'type' => 'type1', - ], - [ - 'value' => 'value2', - 'type' => 'type2', - ], - ], - [ - [ - 'value' => 'value1', - 'type' => 'type1', - ], - [ - 'value' => 'value2', - 'type' => 'type2', - ], - ], - ], - ]; - } - - /** - * @dataProvider dataGetParameterInfo - * @param array $parameters - * @param array $expected - */ - public function testGetParameterInfo(array $parameters, array $expected) { - $instance = $this->getCollection(); - $setParams = []; - foreach ($parameters as $parameter) { - $param = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $param->expects($this->once()) - ->method('getParameterInfo') - ->willReturn($parameter); - $setParams[] = $param; - } - $this->invokePrivate($instance, 'parameters', [$setParams]); - - $this->assertSame([ - 'value' => $expected, - 'type' => 'collection', - ], $instance->getParameterInfo()); - } - - public function testAddParameter() { - $collection = $this->getCollection(); - - /** @var \OCA\Activity\Parameter\IParameter|\PHPUnit_Framework_MockObject_MockObject $parameter1 */ - $parameter1 = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $parameter1->expects($this->atLeastOnce()) - ->method('getParameterInfo') - ->willReturn('One'); - - /** @var \OCA\Activity\Parameter\IParameter|\PHPUnit_Framework_MockObject_MockObject $parameter2 */ - $parameter2 = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $parameter2->expects($this->atLeastOnce()) - ->method('getParameterInfo') - ->willReturn('Two'); - - $this->assertCount(0, $this->invokePrivate($collection, 'parameters')); - $collection->addParameter($parameter1); - $this->assertCount(1, $this->invokePrivate($collection, 'parameters')); - $collection->addParameter($parameter1); - $this->assertCount(1, $this->invokePrivate($collection, 'parameters')); - $collection->addParameter($parameter2); - $this->assertCount(2, $this->invokePrivate($collection, 'parameters')); - } - - public function testFormat() { - $collection = $this->getCollection(); - - /** @var \OCA\Activity\Parameter\IParameter|\PHPUnit_Framework_MockObject_MockObject $parameter1 */ - $parameter1 = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $parameter1->expects($this->once()) - ->method('format') - ->willReturn('OneNull'); - - /** @var \OCA\Activity\Parameter\IParameter|\PHPUnit_Framework_MockObject_MockObject $parameter2 */ - $parameter2 = $this->getMockBuilder('OCA\Activity\Parameter\IParameter') - ->disableOriginalConstructor() - ->getMock(); - $parameter2->expects($this->once()) - ->method('format') - ->willReturn('TwoNull'); - - $this->invokePrivate($collection, 'parameters', [ - [$parameter1, $parameter2] - ]); - - $this->assertSame('OneNullTwoNull', $collection->format()); - } -} diff --git a/tests/Parameter/FactoryTest.php b/tests/Parameter/FactoryTest.php deleted file mode 100644 index 2b9957060..000000000 --- a/tests/Parameter/FactoryTest.php +++ /dev/null @@ -1,228 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Parameter; - -use OCA\Activity\Parameter\Factory; -use OCA\Activity\Tests\TestCase; - -class FactoryTest extends TestCase { - /** @var \OCP\Activity\IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $activityManager; - - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - - /** @var \OCP\IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlGenerator; - - /** @var \OCP\Contacts\IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $contactsManager; - - /** @var \OCA\Activity\ViewInfoCache|\PHPUnit_Framework_MockObject_MockObject */ - protected $infoCache; - - /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ - protected $config; - - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->userManager = $this->getMockBuilder('OCP\IUserManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator') - ->disableOriginalConstructor() - ->getMock(); - - $this->contactsManager = $this->getMockBuilder('OCP\Contacts\IManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->infoCache = $this->getMockBuilder('OCA\Activity\ViewInfoCache') - ->disableOriginalConstructor() - ->getMock(); - - $this->config = $this->getMockBuilder('OCP\IConfig') - ->disableOriginalConstructor() - ->getMock(); - - $this->l = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * @param array $methods - * @param string $user - * @return Factory|\PHPUnit_Framework_MockObject_MockObject - */ - public function getFactory(array $methods = [], $user = 'user') { - $currentUser = $this->getMockBuilder('OCA\Activity\CurrentUser') - ->disableOriginalConstructor() - ->getMock(); - $currentUser->expects($this->once()) - ->method('getUID') - ->willReturn($user); - - if (empty($methods)) { - return new Factory( - $this->activityManager, - $this->userManager, - $this->urlGenerator, - $this->contactsManager, - $this->infoCache, - $this->l, - $currentUser - ); - } else { - return $this->getMockBuilder('OCA\Activity\Parameter\Factory') - ->setConstructorArgs([ - $this->activityManager, - $this->userManager, - $this->urlGenerator, - $this->contactsManager, - $this->infoCache, - $this->l, - $currentUser, - ]) - ->setMethods($methods) - ->getMock(); - } - } - - public function dataSetUser() { - return [ - ['user1', 'user2'], - ['user2', 'user3'], - ]; - } - - /** - * @dataProvider dataSetUser - * - * @param string $initUser - * @param string $setUser - */ - public function testSetUser($initUser, $setUser) { - $factory = $this->getFactory([], $initUser); - $this->assertSame($initUser, $this->invokePrivate($factory, 'user')); - - $factory->setUser($setUser); - $this->assertSame($setUser, $this->invokePrivate($factory, 'user')); - $this->assertNotSame($initUser, $this->invokePrivate($factory, 'user')); - } - - public function testSetL10N() { - $factory = $this->getFactory(); - $this->assertSame($this->l, $this->invokePrivate($factory, 'l')); - - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject $l2 */ - $l2 = $this->getMockBuilder('OCP\IL10N') - ->disableOriginalConstructor() - ->getMock(); - - $factory->setL10n($l2); - $this->assertSame($l2, $this->invokePrivate($factory, 'l')); - $this->assertNotSame($this->l, $this->invokePrivate($factory, 'l')); - } - - public function dataGetParameter() { - return [ - ['parameter1', 'formatter1'], - ['parameter2', 'formatter2'], - ]; - } - - /** - * @dataProvider dataGetParameter - * - * @param string $parameter - * @param string $formatter - */ - public function testGetParameter($parameter, $formatter) { - $factory = $this->getFactory(['getFormatter']); - - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - - $formatterInstance = $this->getMockBuilder('OCA\Activity\Formatter\IFormatter') - ->disableOriginalConstructor() - ->getMock(); - - $factory->expects($this->once()) - ->method('getFormatter') - ->with($formatter) - ->willReturn($formatterInstance); - - $instance = $factory->get($parameter, $event, $formatter); - $this->assertInstanceOf('OCA\Activity\Parameter\IParameter', $instance); - $this->assertInstanceOf('OCA\Activity\Parameter\Parameter', $instance); - - $this->assertSame($parameter, $instance->getParameter()); - $this->assertSame($formatterInstance, $this->invokePrivate($instance, 'formatter')); - } - - public function testCreateCollection() { - $factory = $this->getFactory(); - - $collection1 = $factory->createCollection(); - $this->assertInstanceOf('OCA\Activity\Parameter\IParameter', $collection1); - $this->assertInstanceOf('OCA\Activity\Parameter\Collection', $collection1); - - $collection2 = $factory->createCollection(); - $this->assertNotSame($collection2->getParameter(), $collection1->getParameter(), 'Collection identifier should not be reused'); - } - - public function dataGetFormatter() { - return [ - ['base', 'OCA\Activity\Formatter\BaseFormatter'], - ['file', 'OCA\Activity\Formatter\FileFormatter'], - ['username', 'OCA\Activity\Formatter\UserFormatter'], - ['federated_cloud_id', 'OCA\Activity\Formatter\CloudIDFormatter'], - ]; - } - - /** - * @dataProvider dataGetFormatter - * - * @param string $formatter - * @param string $formatterClass - */ - public function testGetFormatter($formatter, $formatterClass) { - $factory = $this->getFactory(); - $formatterInstance = $this->invokePrivate($factory, 'getFormatter', [$formatter]); - - $this->assertInstanceOf('OCA\Activity\Formatter\IFormatter', $formatterInstance); - $this->assertInstanceOf($formatterClass, $formatterInstance); - } -} diff --git a/tests/Parameter/ParameterTest.php b/tests/Parameter/ParameterTest.php deleted file mode 100644 index ff9b5b21b..000000000 --- a/tests/Parameter/ParameterTest.php +++ /dev/null @@ -1,131 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Parameter; - -use OCA\Activity\Parameter\Parameter; -use OCA\Activity\Tests\TestCase; - -class ParameterTest extends TestCase { - /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject */ - protected $event; - /** @var \OCA\Activity\Formatter\IFormatter|\PHPUnit_Framework_MockObject_MockObject */ - protected $formatter; - - protected function setUp() { - parent::setUp(); - - $this->event = $this->getMockBuilder('OCP\Activity\IEvent') - ->disableOriginalConstructor() - ->getMock(); - $this->formatter = $this->getMockBuilder('OCA\Activity\Formatter\IFormatter') - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * @param string $parameter - * @param string $type - * @return Parameter - */ - public function getParameter($parameter = 'parameter', $type = 'type') { - return new Parameter( - $parameter, - $this->event, - $this->formatter, - $type - ); - } - - public function dataGetParameter() { - return [ - ['parameter1', '', 0, 'parameter1'], - ['parameter2', '', 0, 'parameter2'], - ['parameter1', 'files', 23, 'files#23'], - ['parameter1', 'item', 42, 'item#42'], - ]; - } - - /** - * @dataProvider dataGetParameter - * @param string $parameter - * @param string $objectType - * @param int $objectId - * @param string $expected - */ - public function testGetParameter($parameter, $objectType, $objectId, $expected) { - $instance = $this->getParameter($parameter); - - $this->event->expects($this->atLeastOnce()) - ->method('getObjectType') - ->willReturn($objectType); - $this->event->expects($this->any()) - ->method('getObjectId') - ->willReturn($objectId); - - $this->assertSame($expected, $instance->getParameter()); - } - - public function dataGetParameterInfo() { - return [ - ['parameter1', 'files'], - ['parameter2', 'item'], - ]; - } - - /** - * @dataProvider dataGetParameterInfo - * @param string $parameter - * @param string $type - */ - public function testGetParameterInfo($parameter, $type) { - $instance = $this->getParameter($parameter, $type); - - $this->assertSame([ - 'value' => $parameter, - 'type' => $type, - ], $instance->getParameterInfo()); - } - - public function dataFormat() { - return [ - ['parameter1'], - ['parameter2'], - ]; - } - - /** - * @dataProvider dataFormat - * - * @param string $parameter - */ - public function testFormat($parameter) { - $instance = $this->getParameter($parameter); - - $this->formatter->expects($this->once()) - ->method('format') - ->with($this->event, $parameter) - ->willReturn('formatted()'); - - $this->assertSame('formatted()', $instance->format()); - } -} diff --git a/tests/mock/extension.php b/tests/mock/extension.php deleted file mode 100644 index a44a0ba84..000000000 --- a/tests/mock/extension.php +++ /dev/null @@ -1,180 +0,0 @@ - - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Activity\Tests\Mock; - -use OCP\Activity\IExtension; -use OCP\IL10N; -use OCP\IURLGenerator; - -class Extension implements IExtension { - const TYPE_SHARE_CREATED = 'file_created'; - const TYPE_SHARE_CHANGED = 'file_changed'; - const TYPE_SHARE_DELETED = 'file_deleted'; - const TYPE_SHARE_RESTORED = 'file_restored'; - - /** @var IL10N */ - protected $l; - - /** @var IURLGenerator */ - protected $URLGenerator; - - /** - * @param IL10N $l - * @param IURLGenerator $URLGenerator - */ - public function __construct(IL10N $l, $URLGenerator) { - $this->l = $l; - $this->URLGenerator = $URLGenerator; - } - - /** - * {@inheritdoc} - */ - public function getNotificationTypes($languageCode) { - return [ - 'type1' => 'Type1 description', - 'type2' => 'Type2 description', - ]; - } - - /** - * {@inheritdoc} - */ - public function filterNotificationTypes($types, $filter) { - switch ($filter) { - case 'filter1': - return array_intersect([ - 'type1', - ], $types); - } - return false; - } - - /** - * {@inheritdoc} - */ - public function getDefaultTypes($method) { - if ($method === 'stream') { - return ['type1', 'type2']; - } - - return ['type2']; - } - - /** - * {@inheritdoc} - */ - public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { - if ($app !== 'app1') { - return false; - } - - switch ($text) { - case 'subject1': - return vsprintf('Subject1 #%1$s', $params); - case 'subject2': - return vsprintf('Subject2 @%2$s #%1$s', $params); - case 'subject3': - return vsprintf('Subject3 #%1$s @%2$s', $params); - - default: - return false; - } - } - - /** - * {@inheritdoc} - */ - public function getSpecialParameterList($app, $text) { - if ($app === 'app1') { - switch ($text) { - case 'subject1': - return [0 => 'file']; - case 'subject2': - return [0 => 'file', 1 => 'username']; - } - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function getTypeIcon($type) { - switch ($type) { - case 'type1': - return 'icon-type1'; - - default: - return false; - } - } - - /** - * {@inheritdoc} - */ - public function getGroupParameter($activity) { - if ($activity['app'] === 'app1') { - switch ($activity['subject']) { - case 'subject1': - return 0; - } - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function getNavigation() { - return [ - 'apps' => [ - 'files' => [ - 'id' => 'files', - 'name' => (string) $this->l->t('Files'), - 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => 'files']), - ], - ], - 'top' => [], - ]; - } - - /** - * {@inheritdoc} - */ - public function isFilterValid($filterValue) { - return $filterValue === 'filter1'; - } - - /** - * {@inheritdoc} - */ - public function getQueryForFilter($filter) { - if ($filter === 'app1') { - return ['`app` = ?', ['app1']]; - } - return false; - } -} diff --git a/tests/mock/provider.php b/tests/mock/provider.php new file mode 100644 index 000000000..3a7e5b511 --- /dev/null +++ b/tests/mock/provider.php @@ -0,0 +1,62 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Activity\Tests\Mock; + +use OCP\Activity\IEvent; +use OCP\Activity\IProvider; + +class Provider implements IProvider { + + /** + * @param string $language The language which should be used for translating, e.g. "en" + * @param IEvent $event The current event which should be parsed + * @param IEvent|null $previousEvent A potential previous event which you can combine with the current one. + * To do so, simply use setChildEvent($previousEvent) after setting the + * combined subject on the current event. + * @return IEvent + * @throws \InvalidArgumentException Should be thrown if your provider does not know this event + * @since 11.0.0 + */ + public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { + if ($event->getApp() !== 'app1') { + throw new \InvalidArgumentException(); + } + + switch ($event->getSubject()) { + case 'subject1': + $event->setParsedSubject(vsprintf('Subject1 #%1$s', $event->getSubjectParameters())); + break; + case 'subject2': + $event->setParsedSubject(vsprintf('Subject2 @%2$s #%1$s', $event->getSubjectParameters())); + break; + case 'subject3': + $event->setParsedSubject(vsprintf('Subject3 #%1$s @%2$s', $event->getSubjectParameters())); + break; + + default: + throw new \InvalidArgumentException(); + } + + return $event; + } +} diff --git a/tests/mock/setting1.php b/tests/mock/setting1.php new file mode 100644 index 000000000..01c2bab0b --- /dev/null +++ b/tests/mock/setting1.php @@ -0,0 +1,86 @@ + + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Activity\Tests\Mock; + +use OCP\Activity\ISetting; + +class Setting1 implements ISetting { + + /** + * @return string Lowercase a-z and underscore only identifier + * @since 11.0.0 + */ + public function getIdentifier() { + return 'type1'; + } + + /** + * @return string A translated string + * @since 11.0.0 + */ + public function getName() { + return 'type1'; + } + + /** + * @return int whether the filter should be rather on the top or bottom of + * the admin section. The filters are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * @since 11.0.0 + */ + public function getPriority() { + return 100; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function canChangeStream() { + return false; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledStream() { + return true; + } + + /** + * @return bool True when the option can be changed for the mail + * @since 11.0.0 + */ + public function canChangeMail() { + return false; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledMail() { + return false; + } +} diff --git a/tests/mock/setting2.php b/tests/mock/setting2.php new file mode 100644 index 000000000..c220b98cf --- /dev/null +++ b/tests/mock/setting2.php @@ -0,0 +1,86 @@ + + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Activity\Tests\Mock; + +use OCP\Activity\ISetting; + +class Setting2 implements ISetting { + + /** + * @return string Lowercase a-z and underscore only identifier + * @since 11.0.0 + */ + public function getIdentifier() { + return 'type2'; + } + + /** + * @return string A translated string + * @since 11.0.0 + */ + public function getName() { + return 'type2'; + } + + /** + * @return int whether the filter should be rather on the top or bottom of + * the admin section. The filters are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * @since 11.0.0 + */ + public function getPriority() { + return 100; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function canChangeStream() { + return false; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledStream() { + return true; + } + + /** + * @return bool True when the option can be changed for the mail + * @since 11.0.0 + */ + public function canChangeMail() { + return false; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledMail() { + return true; + } +}