-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
ymir_admin_notices
filter to handle admin notices
- Loading branch information
1 parent
20db281
commit 514679b
Showing
7 changed files
with
342 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Subscriber; | ||
|
||
use Ymir\Plugin\EventManagement\AbstractEventManagerAwareSubscriber; | ||
use Ymir\Plugin\Support\Collection; | ||
|
||
/** | ||
* Subscriber that handles interactions with the WordPress admin. | ||
*/ | ||
class AdminSubscriber extends AbstractEventManagerAwareSubscriber | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'admin_notices' => 'displayAdminNotices', | ||
]; | ||
} | ||
|
||
/** | ||
* Display all admin notices. | ||
*/ | ||
public function displayAdminNotices() | ||
{ | ||
$notices = $this->eventManager->filter('ymir_admin_notices', new Collection()); | ||
|
||
if (!$notices instanceof Collection) { | ||
return; | ||
} | ||
|
||
$notices->map(function ($notice) { | ||
return is_string($notice) ? ['message' => $notice] : $notice; | ||
})->filter(function ($notice) { | ||
return is_array($notice) && !empty($notice['message']); | ||
})->each(function (array $notice) { | ||
$message = $notice['message'] ?? ''; | ||
$type = strtolower($notice['type'] ?? 'info'); | ||
|
||
if (!in_array($type, ['error', 'info', 'success', 'warning'])) { | ||
$type = 'info'; | ||
} | ||
|
||
printf('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>', $type, !empty($notice['dismissible']) ? 'is-dismissible' : '', $message); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Tests\Unit\Subscriber; | ||
|
||
use Ymir\Plugin\Subscriber\AdminSubscriber; | ||
use Ymir\Plugin\Support\Collection; | ||
use Ymir\Plugin\Tests\Mock\EventManagerMockTrait; | ||
use Ymir\Plugin\Tests\Mock\FunctionMockTrait; | ||
use Ymir\Plugin\Tests\Unit\TestCase; | ||
|
||
/** | ||
* @covers \Ymir\Plugin\Subscriber\AdminSubscriber | ||
*/ | ||
class AdminSubscriberTest extends TestCase | ||
{ | ||
use EventManagerMockTrait; | ||
use FunctionMockTrait; | ||
|
||
public function testDisplayAdminNoticesAddsIsDismissibleClassWhenDismissibleIsTrue() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection([ | ||
['message' => 'foo', 'type' => 'warning', 'dismissible' => true], | ||
]); | ||
$printf = $this->getFunctionMock($this->getNamespace(AdminSubscriber::class), 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->once()) | ||
->with( | ||
$this->identicalTo('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>'), | ||
$this->identicalTo('warning'), | ||
$this->identicalTo('is-dismissible'), | ||
$this->identicalTo('foo') | ||
); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesConvertsStringToArray() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection('foo'); | ||
$printf = $this->getFunctionMock($this->getNamespace(AdminSubscriber::class), 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->once()) | ||
->with( | ||
$this->identicalTo('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>'), | ||
$this->identicalTo('info'), | ||
$this->identicalTo(''), | ||
$this->identicalTo('foo') | ||
); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesDefaultsToInfoTypeWithInvalidType() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection([ | ||
['message' => 'foo', 'type' => 'bar'], | ||
]); | ||
$printf = $this->getFunctionMock($this->getNamespace(AdminSubscriber::class), 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->once()) | ||
->with( | ||
$this->identicalTo('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>'), | ||
$this->identicalTo('info'), | ||
$this->identicalTo(''), | ||
$this->identicalTo('foo') | ||
); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesDoesNothingIfCollectionIsEmpty() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$printf = $this->getFunctionMock(AdminSubscriber::class, 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturnArgument(1); | ||
|
||
$printf->expects($this->never()); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesDoesNothingIfCollectionItemIsntAnArrayOrString() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection(1); | ||
$printf = $this->getFunctionMock(AdminSubscriber::class, 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->never()); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesDoesNothingIfFilterDoesntReturnCollection() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$printf = $this->getFunctionMock(AdminSubscriber::class, 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn(null); | ||
|
||
$printf->expects($this->never()); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesDoesntAddIsDismissibleClassWhenDismissibleIsFalse() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection([ | ||
['message' => 'foo', 'type' => 'warning', 'dismissible' => false], | ||
]); | ||
$printf = $this->getFunctionMock($this->getNamespace(AdminSubscriber::class), 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->once()) | ||
->with( | ||
$this->identicalTo('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>'), | ||
$this->identicalTo('warning'), | ||
$this->identicalTo(''), | ||
$this->identicalTo('foo') | ||
); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testDisplayAdminNoticesUsesValidType() | ||
{ | ||
$eventManager = $this->getEventManagerMock(); | ||
$notices = new Collection([ | ||
['message' => 'foo', 'type' => 'warning'], | ||
]); | ||
$printf = $this->getFunctionMock($this->getNamespace(AdminSubscriber::class), 'printf'); | ||
$subscriber = new AdminSubscriber(); | ||
|
||
$eventManager->expects($this->once()) | ||
->method('filter') | ||
->with('ymir_admin_notices', $this->isInstanceOf(Collection::class)) | ||
->willReturn($notices); | ||
|
||
$printf->expects($this->once()) | ||
->with( | ||
$this->identicalTo('<div class="notice notice-%s %s"><p><strong>Ymir:</strong> %s</p></div>'), | ||
$this->identicalTo('warning'), | ||
$this->identicalTo(''), | ||
$this->identicalTo('foo') | ||
); | ||
|
||
$subscriber->setEventManager($eventManager); | ||
|
||
$subscriber->displayAdminNotices(); | ||
} | ||
|
||
public function testGetSubscribedEvents() | ||
{ | ||
$callbacks = AdminSubscriber::getSubscribedEvents(); | ||
|
||
foreach ($callbacks as $callback) { | ||
$this->assertTrue(method_exists(AdminSubscriber::class, is_array($callback) ? $callback[0] : $callback)); | ||
} | ||
|
||
$subscribedEvents = [ | ||
'admin_notices' => 'displayAdminNotices', | ||
]; | ||
|
||
$this->assertSame($subscribedEvents, $callbacks); | ||
} | ||
} |
Oops, something went wrong.