Skip to content

Commit

Permalink
chore(tests): add admin notice tests and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hypeJunction committed Dec 21, 2017
1 parent d5f342d commit a9d3b1a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 16 deletions.
3 changes: 1 addition & 2 deletions actions/admin/delete_admin_notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
$batch = elgg_get_admin_notices([
'limit' => 0,
'batch' => true,
'batch_inc_offset' => false,
]);

$batch->setIncrementOffset(false);

foreach ($batch as $notice) {
$notice->delete();
}
Expand Down
29 changes: 16 additions & 13 deletions engine/classes/Elgg/Database/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class AdminNotices {
* @param string $id A unique ID that your plugin can remember
* @param string $message Body of the message
*
* @return bool
* @return \ElggObject|bool
*/
function add($id, $message) {
public function add($id, $message) {
if (!$id || !$message) {
return false;
}
Expand All @@ -51,7 +51,11 @@ function add($id, $message) {

elgg_set_ignore_access($old_ia);

return (bool) $result;
if (!$result) {
return false;
}

return $admin_notice;
}

/**
Expand All @@ -61,27 +65,26 @@ function add($id, $message) {
*
* @return bool
*/
function delete($id) {
if (!$id) {
return false;
}

public function delete($id = '') {
$result = true;

$notices = elgg_get_entities([
'type' => 'object',
'subtype' => 'admin_notice',
$notices = $this->find([
'metadata_name' => 'admin_notice_id',
'metadata_value' => $id,
'limit' => false,
'batch' => true,
'batch_inc_offset' => false,
]);

$ia = elgg_set_ignore_access(true);

// in case a bad plugin adds many, let it remove them all at once.
foreach ($notices as $notice) {
$result = ($result && $notice->delete());
}

elgg_set_ignore_access($ia);

return $result;
}

Expand All @@ -92,7 +95,7 @@ function delete($id) {
*
* @return \ElggObject[] Admin notices
*/
function find(array $options = []) {
public function find(array $options = []) {
$options = array_merge($options, [
'type' => 'object',
'subtype' => 'admin_notice',
Expand All @@ -109,7 +112,7 @@ function find(array $options = []) {
* @return bool
* @since 1.8.0
*/
function exists($id) {
public function exists($id) {
$old_ia = elgg_set_ignore_access(true);
$notice = elgg_get_entities([
'type' => 'object',
Expand Down
2 changes: 1 addition & 1 deletion engine/lib/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function elgg_get_admins(array $options = []) {
* @param string $id A unique ID that your plugin can remember
* @param string $message Body of the message
*
* @return bool
* @return ElggObject|bool
* @since 1.8.0
*/
function elgg_add_admin_notice($id, $message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Elgg\Actions\Admin;

use Elgg\ActionResponseTestCase;
use Elgg\Http\ErrorResponse;
use Elgg\Http\OkResponse;

/**
* @group ActionsService
* @group AdminNotices
* @group Admin
*/
class AdminNoticesTest extends ActionResponseTestCase {

public function up() {
_elgg_services()->session->setLoggedInUser($this->getAdmin());
}

public function down() {
_elgg_services()->session->removeLoggedInUser();
}

public function testDeletesSingleAdminNotice() {

elgg_delete_admin_notice('zen');

$notice = elgg_add_admin_notice('zen', 'Maybe the forces of the universe be with you');
$this->assertInstanceOf(\ElggObject::class, $notice);
$this->assertEquals('zen', $notice->admin_notice_id);

$this->assertTrue(elgg_admin_notice_exists('zen'));

$response = $this->executeAction('admin/delete_admin_notice', [
'guid' => $notice->guid,
]);

$this->assertInstanceOf(OkResponse::class, $response);

$this->assertFalse(elgg_admin_notice_exists('zen'));
}

public function testIgnoresNonAdminNoticesDuringDeleteAction() {

$object = $this->createObject();

$response = $this->executeAction('admin/delete_admin_notice', [
'guid' => $object->guid,
]);

$this->assertInstanceOf(ErrorResponse::class, $response);
}

public function testDeletesBatchAdminNotices() {

elgg_add_admin_notice('zen1', 'Maybe the forces of the universe be with you');
elgg_add_admin_notice('zen2', 'Maybe the forces of the universe be with you');
elgg_add_admin_notice('zen3', 'Maybe the forces of the universe be with you');

$count_zens = function() {
return elgg_get_admin_notices([
'metadata_name_value_pairs' => [
'name' => 'admin_notice_id',
'value' => 'zen%',
'operand' => 'LIKE',
],
'count' => true,
]);
};

$this->assertEquals(3, $count_zens());

$response = $this->executeAction('admin/delete_admin_notices');

$this->assertInstanceOf(OkResponse::class, $response);

$this->assertEquals(0, $count_zens());
}

}

0 comments on commit a9d3b1a

Please sign in to comment.