/** * 1. Register a page handler for `/foo` * 2. Register a plugin hook that uses the "handler" result param * to route all `/bar/*` requests to the `/foo` handler. * 3. Route a request for a `/bar` page. * 4. Check that the `/foo` handler was called. */ function testRouteSupportsSettingHandlerInHookResultForBackwardsCompatibility() { $this->router->registerPageHandler('foo', array($this, 'foo_page_handler')); $this->hooks->registerHandler('route', 'bar', array($this, 'bar_route_handler')); $query = http_build_query(array('__elgg_uri' => 'bar/baz')); ob_start(); $this->router->route(\Elgg\Http\Request::create("http://localhost/?{$query}")); ob_end_clean(); $this->assertEquals(1, $this->fooHandlerCalls); }
/** * Constructor * * @param PluginHooksService $hooks Hooks service * @param SystemMessagesService $msgs System messages service * @param Input $input Input service * @param Config $amdConfig AMD config */ public function __construct(PluginHooksService $hooks, SystemMessagesService $msgs, Input $input, Config $amdConfig) { $this->hooks = $hooks; $this->msgs = $msgs; $this->input = $input; $this->amd_config = $amdConfig; if ($this->input->get('elgg_fetch_messages', true)) { $message_filter = [$this, 'appendMessages']; $this->hooks->registerHandler(AjaxResponse::RESPONSE_HOOK, 'all', $message_filter, 999); } if ($this->input->get('elgg_fetch_deps', true)) { $deps_filter = [$this, 'appendDeps']; $this->hooks->registerHandler(AjaxResponse::RESPONSE_HOOK, 'all', $deps_filter, 999); } }
public function testCanAlterViewOutput() { $this->hooks->registerHandler('view', 'js/interpreted.js', function ($h, $t, $v, $p) { return '// Hello'; }); $this->assertEquals("// Hello", $this->views->renderView('js/interpreted.js')); }
/** * @group IconService */ public function testCanListenToIconsSavedHook() { $file = new \ElggFile(); $file->owner_guid = 1; $file->setFilename('600x300.jpg'); $this->hooks->registerHandler('entity:icon:saved', 'object', function ($hook, $type, $return, $params) { // make sure we passed in documented params if (!$params['entity'] instanceof \ElggEntity) { return; } if (!isset($params['x1']) || !isset($params['y1']) || !isset($params['x2']) || !isset($params['y2'])) { return; } _elgg_services()->iconService->deleteIcon($params['entity']); }); $this->assertTrue($this->hooks->hasHandler('entity:icon:saved', 'object')); $service = $this->createService(); _elgg_services()->setValue('iconService', $service); $service->saveIconFromElggFile($this->entity, $file); // icons were deleted by the hook $this->assertFalse($service->hasIcon($this->entity, 'master')); $this->assertFalse($service->hasIcon($this->entity, 'large')); $this->assertFalse($service->hasIcon($this->entity, 'medium')); $this->assertFalse($service->hasIcon($this->entity, 'small')); $this->assertFalse($service->hasIcon($this->entity, 'tiny')); $this->assertFalse($service->hasIcon($this->entity, 'topbar')); }
public function testCanFilterResponseBuilder() { $this->hooks->registerHandler('response', 'action:output4', function ($hook, $type, $response, $params) { $this->assertEquals('response', $hook); $this->assertEquals('action:output4', $type); $this->assertEquals($response, $params); $this->assertInstanceOf(OkResponse::class, $response); $response->setContent('good bye'); $response->setStatusCode(ELGG_HTTP_BAD_REQUEST); return $response; }); $this->assertTrue($this->actions->register('output4', "{$this->actionsDir}/output4.php", 'public')); $this->request = $this->prepareHttpRequest('action/output4', 'POST', [], 2, true); $this->createService(); set_input('output', ['foo', 'bar']); set_input('system_message', 'success'); set_input('forward_reason', ELGG_HTTP_OK); set_input('forward_url', 'index'); $this->route(); $response = _elgg_services()->responseFactory->getSentResponse(); $this->assertInstanceOf(Response::class, $response); $this->assertEquals(ELGG_HTTP_BAD_REQUEST, $response->getStatusCode()); $this->assertContains('application/json', $response->headers->get('Content-Type')); //$this->assertContains('charset=utf-8', strtolower($response->headers->get('Content-Type'))); $output = json_encode(['error' => 'good bye']); $this->assertEquals($output, $response->getContent()); }
function testRouteOverridenFromHook() { $this->router->registerPageHandler('foo', array($this, 'foo_page_handler')); $this->hooks->registerHandler('route', 'foo', array($this, 'bar_route_override')); $query = http_build_query(array(Application::GET_PATH_KEY => 'foo')); ob_start(); $this->router->route(\Elgg\Http\Request::create("http://localhost/?{$query}")); $result = ob_get_contents(); ob_end_clean(); $this->assertEquals("Page handler override from hook", $result); $this->assertEquals(0, $this->fooHandlerCalls); }
/** * @group InstantNotificationsService */ public function testCanNotifyUserViaCustomMethods() { $object = $this->getTestObject(); $from = $this->mocks()->getUser(); $to1 = $this->mocks()->getUser(); create_metadata($to1->guid, 'notification:method:test_method', true, '', $to1->guid, ACCESS_PUBLIC); $to2 = $this->mocks()->getUser(); create_metadata($to2->guid, 'notification:method:test_method', true, '', $to2->guid, ACCESS_PUBLIC); $subject = 'Test message'; $body = 'Lorem ipsum'; $this->hooks->registerHandler('send', 'notification:test_method', [Values::class, 'getFalse']); $this->hooks->registerHandler('send', 'notification:test_method2', [Values::class, 'getTrue']); $this->setupServices(); $this->notifications->registerMethod('test_method'); $this->notifications->registerMethod('test_method2'); $expected = [$to1->guid => ['test_method2' => true], $to2->guid => ['test_method2' => true]]; $this->assertEquals($expected, notify_user([$to1->guid, $to2->guid, 0], $from->guid, $subject, $body, [], 'test_method2')); }
/** * @group AjaxService */ public function testCanFilterResponseToAjax2ViewRequestForARegisteredFormView() { $this->hooks->registerHandler('response', 'form:query_view', function ($hook, $type, $response, $params) { $this->assertEquals('response', $hook); $this->assertEquals('form:query_view', $type); $this->assertEquals($response, $params); $this->assertInstanceOf(OkResponse::class, $response); return elgg_error_response('good bye', REFERRER, ELGG_HTTP_BAD_REQUEST); }); $vars = ['query_value' => 'hello']; $this->request = $this->prepareHttpRequest('ajax/form/query_view', 'GET', $vars, 2); $this->createService(); elgg_register_ajax_view('form/query_view'); $this->route(); $response = _elgg_services()->responseFactory->getSentResponse(); $this->assertInstanceOf(Response::class, $response); $this->assertEquals(ELGG_HTTP_BAD_REQUEST, $response->getStatusCode()); $this->assertContains('application/json', $response->headers->get('Content-Type')); $output = json_encode(['error' => 'good bye'], ELGG_JSON_ENCODING); $this->assertEquals($output, $response->getContent()); }
/** * @group UserCapabilities */ public function testCanOverrideContainerLogicWithAHook() { $owner = $this->mocks()->getUser(); $entity = $this->mocks()->getObject(['owner_guid' => $owner->guid]); $this->assertTrue($entity->canWriteToContainer($owner->guid, 'object', 'bar')); $this->hooks->registerHandler('container_logic_check', 'object', function ($hook, $type, $return, $params) use($entity, $owner) { $this->assertInstanceOf(ElggEntity::class, $params['container']); $this->assertInstanceOf(ElggUser::class, $params['user']); $this->assertEquals($entity, $params['container']); $this->assertEquals($owner, $params['user']); $this->assertEquals('object', $type); $this->assertEquals('bar', $params['subtype']); $this->assertNull($return); return false; }); $this->assertFalse($entity->canWriteToContainer($owner->guid, 'object', 'bar')); // make sure container permission hooks are not triggered $this->hooks->registerHandler('container_permissions_check', 'object', function () { return true; }); $this->assertFalse($entity->canWriteToContainer($owner->guid, 'object', 'bar')); }