/** * Test controller hooks. * * @return void */ public function testControllerHooks() { Hook::helper('TestPlugin.Entity', '*'); Hook::helper('TestPlugin.Good', 'Pages'); Hook::helper('TestPlugin.String', ['Display']); Hook::helper('TestPlugin.Simple', ['Common', 'Pages']); Hook::component('TestPlugin.Good', '*'); Hook::component('TestPlugin.Image', 'Pages'); Hook::component('TestPlugin.Resize', ['Display']); Hook::component('TestPlugin.Simple', ['Common', 'Pages']); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Pages controller hooks. $request = new Request(['params' => ['plugin' => 'TestPlugin', 'controller' => 'Pages', 'action' => 'index', 'pass' => []]]); $controller = new PagesController($request); $view = $controller->createView('Union\\Core\\View\\AjaxView'); $components = $controller->components(); $helpers = $view->helpers(); /** Test hook helpers */ $this->assertTrue(in_array('Entity', $helpers->loaded())); $this->assertTrue(in_array('Simple', $helpers->loaded())); $this->assertNotTrue(in_array('String', $helpers->loaded())); $this->assertInstanceOf('Union\\Core\\View\\Helper\\UrlHelper', $helpers->get('Url')); $this->assertInstanceOf('Union\\Core\\View\\Helper\\HtmlHelper', $helpers->get('Html')); /** Test hook components */ $this->assertTrue(in_array('Csrf', $components->loaded())); $this->assertTrue(in_array('Flash', $components->loaded())); $this->assertTrue(in_array('Cookie', $components->loaded())); $this->assertTrue(in_array('Security', $components->loaded())); $this->assertTrue(in_array('Paginator', $components->loaded())); $this->assertTrue(in_array('RequestHandler', $components->loaded())); $this->assertTrue(in_array('Good', $components->loaded())); $this->assertTrue(in_array('Simple', $components->loaded())); $this->assertNotTrue(in_array('String', $components->loaded())); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Display controller hooks. $request = new Request(['params' => ['plugin' => 'TestPlugin', 'controller' => 'Display', 'action' => 'index', 'pass' => []]]); $controller = new DisplayController($request); $view = $controller->createView('Union\\Core\\View\\AjaxView'); $components = $controller->components(); $helpers = $view->helpers(); /** Test hook helpers */ $this->assertTrue(in_array('Url', $helpers->loaded())); $this->assertTrue(in_array('Html', $helpers->loaded())); $this->assertTrue(in_array('String', $helpers->loaded())); $this->assertNotTrue(in_array('Good', $helpers->loaded())); $this->assertNotTrue(in_array('Simple', $helpers->loaded())); /** Test hook components */ $this->assertTrue(in_array('Good', $components->loaded())); $this->assertTrue(in_array('Resize', $components->loaded())); $this->assertNotTrue(in_array('Image', $components->loaded())); $this->assertNotTrue(in_array('Simple', $components->loaded())); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Common controller hooks. $request = new Request(['params' => ['plugin' => 'TestPlugin', 'controller' => 'Common', 'action' => 'index', 'pass' => []]]); $controller = new CommonController($request); $view = $controller->createView('Union\\Core\\View\\AjaxView'); $components = $controller->components(); $helpers = $view->helpers(); /** Test hook helpers */ $this->assertTrue(in_array('Url', $helpers->loaded())); $this->assertTrue(in_array('Html', $helpers->loaded())); $this->assertTrue(in_array('Simple', $helpers->loaded())); $this->assertNotTrue(in_array('Good', $helpers->loaded())); $this->assertNotTrue(in_array('String', $helpers->loaded())); /** Test hook components */ $this->assertTrue(in_array('Good', $components->loaded())); $this->assertTrue(in_array('Simple', $components->loaded())); $this->assertNotTrue(in_array('Image', $components->loaded())); $this->assertNotTrue(in_array('Resize', $components->loaded())); }
/** * Test success toggle field. * * @return void */ public function testFieldToggleSuccess() { $request = new Request(['params' => ['plugin' => 'TestPlugin', 'controller' => 'Pages', 'action' => 'toggle', 'pass' => []], 'environment' => ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest']]); $entityId = 1; $table = TableRegistry::get('TestPlugin.Pages'); $controller = new PagesController($request); $this->assertSame(1, $table->get($entityId)->get('status')); $controller->toggle($entityId, 1); $view = $controller->createView('Union\\Core\\View\\AppView'); $this->assertSame(0, $table->get($entityId)->get('status')); $actual = json_decode($view->render(), true); $this->assertSame(4, count($actual)); $this->assertSame(1, $actual['id']); $this->assertFalse($actual['status']); $this->assertTrue(array_key_exists('id', $actual)); $this->assertTrue(array_key_exists('url', $actual)); $this->assertTrue(array_key_exists('html', $actual)); $this->assertTrue(array_key_exists('status', $actual)); $this->assertSame('/test_plugin/pages/toggle/1', $actual['url']); }