Skip to content

Commit

Permalink
Merge pull request #20539 from owncloud/notification-api-adjustment
Browse files Browse the repository at this point in the history
Notification api update
  • Loading branch information
DeepDiver1975 committed Nov 17, 2015
2 parents 56f44a4 + 40d5d55 commit 705d208
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 141 deletions.
46 changes: 24 additions & 22 deletions lib/private/notification/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Action implements IAction {
/** @var string */
protected $icon;

/** @var bool */
protected $primary;

/**
* Constructor
*/
Expand Down Expand Up @@ -94,6 +97,27 @@ public function getParsedLabel() {
return $this->labelParsed;
}

/**
* @param $primary bool
* @throws \InvalidArgumentException if $primary is invalid
* @since 9.0.0
*/
public function setPrimary($primary) {
if (!is_bool($primary)) {
throw new \InvalidArgumentException('The given primary option is invalid');
}

$this->primary = $primary;
}

/**
* @return bool
* @since 9.0.0
*/
public function isPrimary() {
return $this->primary;
}

/**
* @param string $link
* @param string $requestType
Expand Down Expand Up @@ -129,28 +153,6 @@ public function getRequestType() {
return $this->requestType;
}

/**
* @param string $icon
* @return $this
* @throws \InvalidArgumentException if the icon is invalid
* @since 8.2.0
*/
public function setIcon($icon) {
if (!is_string($icon) || $icon === '' || isset($icon[64])) {
throw new \InvalidArgumentException('The given icon is invalid');
}
$this->icon = $icon;
return $this;
}

/**
* @return string
* @since 8.2.0
*/
public function getIcon() {
return $this->icon;
}

/**
* @return bool
*/
Expand Down
27 changes: 13 additions & 14 deletions lib/private/notification/iaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public function setParsedLabel($label);
*/
public function getParsedLabel();

/**
* @param $primary bool
* @throws \InvalidArgumentException if $primary is invalid
* @since 9.0.0
*/
public function setPrimary($primary);

/**
* @return bool
* @since 9.0.0
*/
public function isPrimary();

/**
* @param string $link
* @param string $requestType
Expand All @@ -81,20 +94,6 @@ public function getLink();
*/
public function getRequestType();

/**
* @param string $icon
* @return $this
* @throws \InvalidArgumentException if the icon is invalid
* @since 8.2.0
*/
public function setIcon($icon);

/**
* @return string
* @since 8.2.0
*/
public function getIcon();

/**
* @return bool
* @since 8.2.0
Expand Down
14 changes: 0 additions & 14 deletions lib/private/notification/inotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,6 @@ public function setLink($link);
*/
public function getLink();

/**
* @param string $icon
* @return $this
* @throws \InvalidArgumentException if the icon are invalid
* @since 8.2.0
*/
public function setIcon($icon);

/**
* @return string
* @since 8.2.0
*/
public function getIcon();

/**
* @return IAction
* @since 8.2.0
Expand Down
46 changes: 24 additions & 22 deletions lib/private/notification/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class Notification implements INotification {
/** @var array */
protected $actionsParsed;

/** @var bool */
protected $hasPrimaryAction;

/** @var bool */
protected $hasPrimaryParsedAction;

/**
* Constructor
*/
Expand Down Expand Up @@ -329,28 +335,6 @@ public function getLink() {
return $this->link;
}

/**
* @param string $icon
* @return $this
* @throws \InvalidArgumentException if the icon are invalid
* @since 8.2.0
*/
public function setIcon($icon) {
if (!is_string($icon) || $icon === '' || isset($icon[64])) {
throw new \InvalidArgumentException('The given icon is invalid');
}
$this->icon = $icon;
return $this;
}

/**
* @return string
* @since 8.2.0
*/
public function getIcon() {
return $this->icon;
}

/**
* @return IAction
* @since 8.2.0
Expand All @@ -369,6 +353,15 @@ public function addAction(IAction $action) {
if (!$action->isValid()) {
throw new \InvalidArgumentException('The given action is invalid');
}

if ($action->isPrimary()) {
if ($this->hasPrimaryAction) {
throw new \InvalidArgumentException('The notification already has a primary action');
}

$this->hasPrimaryAction = true;
}

$this->actions[] = $action;
return $this;
}
Expand All @@ -391,6 +384,15 @@ public function addParsedAction(IAction $action) {
if (!$action->isValidParsed()) {
throw new \InvalidArgumentException('The given parsed action is invalid');
}

if ($action->isPrimary()) {
if ($this->hasPrimaryParsedAction) {
throw new \InvalidArgumentException('The notification already has a primary action');
}

$this->hasPrimaryParsedAction = true;
}

$this->actionsParsed[] = $action;
return $this;
}
Expand Down
41 changes: 0 additions & 41 deletions tests/lib/notification/actiontest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,47 +171,6 @@ public function testSetLinkInvalid($link, $type) {
$this->action->setLink($link, $type);
}

public function dataSetIcon() {
return [
['test1'],
[str_repeat('a', 1)],
[str_repeat('a', 64)],
];
}

/**
* @dataProvider dataSetIcon
* @param string $icon
*/
public function testSetIcon($icon) {
$this->assertSame('', $this->action->getIcon());
$this->action->setIcon($icon);
$this->assertSame($icon, $this->action->getIcon());
}

public function dataSetIconInvalid() {
return [
[true],
[false],
[0],
[1],
[''],
[str_repeat('a', 65)],
[[]],
[[str_repeat('a', 65)]],
];
}

/**
* @dataProvider dataSetIconInvalid
* @param string $icon
*
* @expectedException \InvalidArgumentException
*/
public function testSetIconInvalid($icon) {
$this->action->setIcon($icon);
}

public function testIsValid() {
$this->assertFalse($this->action->isValid());
$this->assertFalse($this->action->isValidParsed());
Expand Down
64 changes: 36 additions & 28 deletions tests/lib/notification/notificationtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,34 +371,6 @@ public function testSetLinkInvalid($link) {
$this->notification->setLink($link);
}

public function dataSetIcon() {
return $this->dataValidString(64);
}

/**
* @dataProvider dataSetIcon
* @param string $icon
*/
public function testSetIcon($icon) {
$this->assertSame('', $this->notification->getIcon());
$this->notification->setIcon($icon);
$this->assertSame($icon, $this->notification->getIcon());
}

public function dataSetIconInvalid() {
return $this->dataInvalidString(64);
}

/**
* @dataProvider dataSetIconInvalid
* @param mixed $icon
*
* @expectedException \InvalidArgumentException
*/
public function testSetIconInvalid($icon) {
$this->notification->setIcon($icon);
}

public function testCreateAction() {
$action = $this->notification->createAction();
$this->assertInstanceOf('OC\Notification\IAction', $action);
Expand Down Expand Up @@ -438,6 +410,24 @@ public function testAddActionInvalid() {
$this->notification->addAction($action);
}

public function testAddActionSecondPrimary() {
/** @var \OC\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
$action = $this->getMockBuilder('OC\Notification\IAction')
->disableOriginalConstructor()
->getMock();
$action->expects($this->exactly(2))
->method('isValid')
->willReturn(true);
$action->expects($this->exactly(2))
->method('isPrimary')
->willReturn(true);

$this->notification->addAction($action);

$this->setExpectedException('\InvalidArgumentException');
$this->notification->addAction($action);
}

public function testAddParsedAction() {
/** @var \OC\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
$action = $this->getMockBuilder('OC\Notification\IAction')
Expand Down Expand Up @@ -472,6 +462,24 @@ public function testAddParsedActionInvalid() {
$this->notification->addParsedAction($action);
}

public function testAddActionSecondParsedPrimary() {
/** @var \OC\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */
$action = $this->getMockBuilder('OC\Notification\IAction')
->disableOriginalConstructor()
->getMock();
$action->expects($this->exactly(2))
->method('isValidParsed')
->willReturn(true);
$action->expects($this->exactly(2))
->method('isPrimary')
->willReturn(true);

$this->notification->addParsedAction($action);

$this->setExpectedException('\InvalidArgumentException');
$this->notification->addParsedAction($action);
}

public function dataIsValid() {
return [
[false, '', false],
Expand Down

0 comments on commit 705d208

Please sign in to comment.