/**
  * The extension can translate a given message to the requested languages.
  * If no translation is available false is to be returned.
  *
  * @param string $app
  * @param string $text
  * @param array $params
  * @param boolean $stripPath
  * @param boolean $highlightParams
  * @param string $languageCode
  * @return string|false
  */
 public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode)
 {
     if ($app === 'announcementcenter') {
         $l = $this->languageFactory->get('announcementcenter', $languageCode);
         list(, $id) = explode('#', $text);
         try {
             $announcement = $this->manager->getAnnouncement($id, $highlightParams);
         } catch (\InvalidArgumentException $e) {
             return (string) $l->t('Announcement does not exist anymore', $params);
         }
         if (strpos($text, 'announcementmessage#') === 0) {
             return $announcement['message'];
         }
         if ($highlightParams) {
             $params[] = '<strong>' . $announcement['subject'] . '</strong>';
         } else {
             $params[] = $announcement['subject'];
         }
         if ($announcement['author'] === $this->activityManager->getCurrentUserId()) {
             array_shift($params);
             return (string) $l->t('You announced %s', $params);
         }
         return (string) $l->t('%s announced %s', $params);
     }
     return false;
 }
 /**
  * @param int $id
  * @return Response
  */
 public function delete($id)
 {
     $this->manager->delete($id);
     $notification = $this->notificationManager->createNotification();
     $notification->setApp('announcementcenter')->setObject('announcement', $id);
     $this->notificationManager->markProcessed($notification);
     return new Response();
 }
 /**
  * @param INotification $notification
  * @param string $languageCode The code of the language that should be used to prepare the notification
  * @return INotification
  * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  */
 public function prepare(INotification $notification, $languageCode)
 {
     if ($notification->getApp() !== 'announcementcenter') {
         // Not my app => throw
         throw new \InvalidArgumentException();
     }
     // Read the language from the notification
     $l = $this->l10nFactory->get('announcementcenter', $languageCode);
     switch ($notification->getSubject()) {
         // Deal with known subjects
         case 'announced':
             $params = $notification->getSubjectParameters();
             $announcement = $this->manager->getAnnouncement($notification->getObjectId(), false);
             $params[] = $this->prepareMessage($announcement['subject']);
             $notification->setParsedMessage($this->prepareMessage($announcement['message']))->setParsedSubject((string) $l->t('%1$s announced “%2$s”', $params));
             return $notification;
         default:
             // Unknown subject => Unknown notification => throw
             throw new \InvalidArgumentException();
     }
 }
 public function testAdd()
 {
     $this->manager->expects($this->once())->method('announce')->with('subject', 'message', 'author', $this->anything())->willReturn(['author' => 'author', 'subject' => 'subject', 'message' => 'message', 'time' => time(), 'id' => 10]);
     $this->userManager->expects($this->once())->method('get')->with('author')->willReturn($this->getUserMock('author', 'Author'));
     $controller = $this->getController(['createPublicity']);
     $controller->expects($this->once())->method('createPublicity')->with(10, 'author', $this->anything());
     $response = $controller->add('subject', 'message');
     $this->assertInstanceOf('OCP\\AppFramework\\Http\\JSONResponse', $response);
     $data = $response->getData();
     $this->assertArrayHasKey('time', $data);
     $this->assertInternalType('int', $data['time']);
     unset($data['time']);
     $this->assertEquals(['author' => 'Author', 'author_id' => 'author', 'subject' => 'subject', 'message' => 'message', 'id' => 10], $data);
 }
 /**
  * @dataProvider dataTranslate
  *
  * @param string $app
  * @param string $text
  * @param array $params
  * @param bool $stripPath
  * @param bool $highlightParams
  * @param string $languageCode
  * @param mixed $managerReturn
  * @param string $currentUser
  * @param mixed $expected
  */
 public function testTranslate($app, $text, $params, $stripPath, $highlightParams, $languageCode, $managerReturn, $currentUser, $expected)
 {
     if ($managerReturn === null) {
         $this->manager->expects($this->never())->method('getAnnouncement');
     } else {
         $this->factory->expects($this->any())->method('get')->with('announcementcenter', $languageCode)->willReturn($this->l);
         $this->activity->expects($this->any())->method('getCurrentUserId')->willReturn($currentUser);
         if ($managerReturn === false) {
             $this->manager->expects($this->once())->method('getAnnouncement')->with(10, $highlightParams)->willThrowException(new \InvalidArgumentException());
         } else {
             $this->manager->expects($this->once())->method('getAnnouncement')->with(10, $highlightParams)->willReturn($managerReturn);
         }
     }
     $this->assertSame($expected, $this->extension->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode));
 }
 public function te1stAnnouncement()
 {
     $subject = 'subject' . "\n<html>";
     $message = 'message' . "\n<html>";
     $author = 'author';
     $time = time() - 10;
     $announcement = $this->manager->announce($subject, $message, $author, $time);
     $this->assertInternalType('int', $announcement['id']);
     $this->assertGreaterThan(0, $announcement['id']);
     $this->assertSame('subject &lt;html&gt;', $announcement['subject']);
     $this->assertSame('message<br />&lt;html&gt;', $announcement['message']);
     $this->assertSame('author', $announcement['author']);
     $this->assertSame($time, $announcement['time']);
     $this->assertEquals($announcement, $this->manager->getAnnouncement($announcement['id']));
     $this->assertEquals($announcement, $this->manager->getAnnouncement($announcement['id']));
     $this->assertEquals([$announcement], $this->manager->getAnnouncements(1));
     $this->manager->delete($announcement['id']);
     try {
         $this->manager->getAnnouncement($announcement['id']);
         $this->fail('Failed to delete the announcement');
     } catch (\InvalidArgumentException $e) {
         $this->assertInstanceOf('InvalidArgumentException', $e);
     }
 }