Skip to content

Commit

Permalink
Merge pull request #13703 from jdalsem/4.0-tests
Browse files Browse the repository at this point in the history
4.0 fixes
  • Loading branch information
jeabakker committed Oct 1, 2021
2 parents e67bf90 + 4222ac9 commit d6c1100
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 23 deletions.
2 changes: 1 addition & 1 deletion engine/classes/Elgg/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public function run() {

$forward_url = $this->_services->hooks->trigger('forward', $ex->getCode(), $hook_params, $forward_url);

if ($forward_url) {
if ($forward_url && !$request->isXmlHttpRequest()) {
if ($ex->getMessage()) {
$this->_services->systemMessages->addErrorMessage($ex->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function __invoke(\Elgg\Hook $hook) {
return;
}

if (!elgg_is_logged_in()) {
return;
}

$link_classes = [];
if ($hook->getParam('name') === 'title') {
$link_classes = [
Expand All @@ -41,19 +45,30 @@ public function __invoke(\Elgg\Hook $hook) {
}

$has_subscriptions = $entity->hasSubscriptions();

// subscribe
$result[] = \ElggMenuItem::factory([
$subscribe_options = [
'name' => 'entity_subscribe',
'icon' => 'bell',
'text' => elgg_echo('entity:subscribe'),
'href' => elgg_generate_action_url('entity/subscribe', [
'guid' => $entity->guid,
]),
'href' => false,
'item_class' => $has_subscriptions ? 'hidden' : '',
'link_class' => $link_classes,
'data-toggle' => 'entity_mute',
]);
];

// check if it makes sense to enable the subscribe button
$has_preferences = !empty(array_keys(array_filter(elgg_get_logged_in_user_entity()->getNotificationSettings())));
if ($has_preferences) {
$subscribe_options['href'] = elgg_generate_action_url('entity/subscribe', [
'guid' => $entity->guid,
]);
$subscribe_options['data-toggle'] = 'entity_mute';
} else {
$subscribe_options['link_class'][] = 'elgg-state-disabled';
$subscribe_options['title']= elgg_echo('entity:subscribe:disabled');
}

$result[] = \ElggMenuItem::factory($subscribe_options);

// mute
$result[] = \ElggMenuItem::factory([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Elgg\Helpers\Application;

use Elgg\Request;

/**
* @see Elgg\ApplicationUnitTest
*/
class FooNonHttpExceptionController {
public function __invoke(Request $request) {
throw new \InvalidArgumentException($request->getParam('echo'));
}
}
44 changes: 44 additions & 0 deletions engine/tests/phpunit/unit/Elgg/ApplicationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Elgg\Views\TableColumn\ColumnFactory;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Elgg\Helpers\Application\FooNonHttpExceptionController;

/**
* @group UnitTests
Expand Down Expand Up @@ -502,4 +503,47 @@ function testHandlesRequestToRegisteredActionRoute() {
$this->assertEquals($output, $response->getContent());
$this->assertEquals(elgg_normalize_site_url('/phpunit'), $response->getTargetUrl());
}

function testHandlesRequestToRegisteredActionRouteWithHttpExceptionInXhr() {

$request = $this->prepareHttpRequest('action/foo', 'POST', [], 1, true);

$app = $this->createMockApplication([
'request' => $request,
]);

$app->_services->routes->register('action:foo', [
'path' => '/action/foo',
'controller' => FooController::class,
'middleware' => [
\Elgg\Router\Middleware\Gatekeeper::class,
],
]);

ob_start();
$response = $app->index();
$output = ob_get_clean();

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\Response::class, $response);
$this->assertEquals(ELGG_HTTP_UNAUTHORIZED, $response->getStatusCode());
}

function testHandlesRequestToRegisteredActionRouteWithNonHttpExceptionInXhr() {

$request = $this->prepareHttpRequest('action/foo', 'POST', ['echo' => 'Hello'], 1, true);

$app = $this->createMockApplication([
'request' => $request,
]);

$app->_services->routes->register('action:foo', [
'path' => '/action/foo',
'controller' => FooNonHttpExceptionController::class,
]);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Hello');

$app->index();
}
}
1 change: 1 addition & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@
'entity:delete:fail' => '%s could not be deleted.',

'entity:subscribe' => "Subscribe",
'entity:subscribe:disabled' => "Your default notification settings prevent you from subscribing to this content",
'entity:subscribe:success' => "You've successfully subscribed to %s",
'entity:subscribe:fail' => "An error occured while subscribing to %s",

Expand Down
20 changes: 15 additions & 5 deletions mod/system_log/classes/Elgg/SystemLog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ public static function log(\Elgg\Event $event) {
*
* @param \Elgg\Event $event 'all', 'all'
*
* @return true
* @return void
*/
public static function listen(\Elgg\Event $event) {
if (($event->getType() != 'systemlog') && ($event->getName() != 'log')) {
elgg_trigger_event('log', 'systemlog', ['object' => $event->getObject(), 'event' => $event->getName()]);
$type = $event->getType();
$name = $event->getName();

if ($type === 'systemlog' && $name === 'log') {
return;
}

return true;

// ignore before and after events if there are no event handlers registered
if (strpos($name, ':after') > 0 || strpos($name, ':before') > 0) {
if (!elgg()->events->hasHandler($name, $type) && !elgg()->events->hasHandler($name, 'all')) {
return;
}
}

elgg_trigger_event('log', 'systemlog', ['object' => $event->getObject(), 'event' => $event->getName()]);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions views/default/object/elements/summary/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

$entity = elgg_extract('entity', $vars);
$show_entity_menu_default = true;
if ($entity instanceof \ElggEntity && stripos(current_page_url(), $entity->getURL()) !== false) {
$show_entity_menu_default = false;
if ($entity instanceof \ElggEntity) {
$entity_url = $entity->getURL();
if (!empty($entity_url) && stripos(current_page_url(), $entity_url) !== false) {
// probably on a full view of an entity
$show_entity_menu_default = false;
}
}

$show_entity_menu = elgg_extract('show_entity_menu', $vars, $show_entity_menu_default);
Expand Down
12 changes: 4 additions & 8 deletions views/default/page/components/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ define(['jquery', 'elgg', 'elgg/Ajax'], function ($, elgg, Ajax) {
}
}).done(function (output, statusText, jqXHR) {
$tab.data('loaded', true);
$target.removeClass('elgg-ajax-loader');
if (jqXHR.AjaxData.status === -1) {
$target.html(elgg.echo('ajax:error'));
return;
} else {
$target.html(output);
}

$target.removeClass('elgg-ajax-loader').html(output);

changeTab($tab, true);
}).fail(function() {
$target.removeClass('elgg-ajax-loader').html(elgg.echo('ajax:error'));
});
}
};
Expand Down

0 comments on commit d6c1100

Please sign in to comment.