| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- App::uses('CommonComponent', 'Tools.Controller/Component');
- App::uses('Component', 'Controller');
- App::uses('AppController', 'Controller');
- App::uses('AppModel', 'Model');
- /**
- * 2010-11-10 ms
- */
- class CommonComponentTest extends CakeTestCase {
- public $fixtures = array('core.cake_session', 'plugin.tools.tools_user', 'plugin.tools.role');
- public function setUp() {
- parent::setUp();
- CakeSession::delete('Auth');
- $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
- $this->Controller->constructClasses();
- $this->Controller->startupProcess();
- }
- public function tearDown() {
- parent::tearDown();
- unset($this->Controller->Common);
- unset($this->Controller);
- }
- /**
- * CommonComponentTest::testLoadHelper()
- *
- * @return void
- */
- public function testLoadHelper() {
- $this->assertTrue(!in_array('Text', $this->Controller->helpers));
- $this->Controller->Common->loadHelper('Text');
- $this->assertTrue(in_array('Text', $this->Controller->helpers));
- }
- /**
- * CommonComponentTest::testLoadComponent()
- *
- * @return void
- */
- public function testLoadComponent() {
- $this->assertTrue(!isset($this->Controller->Test));
- $this->Controller->Common->loadComponent('Test');
- $this->assertTrue(isset($this->Controller->Test));
- # with plugin
- $this->Controller->Calendar = null;
- $this->assertTrue(!isset($this->Controller->Calendar));
- $this->Controller->Common->loadComponent('Tools.Calendar');
- $this->assertTrue(isset($this->Controller->Calendar));
- # with options
- $this->Controller->Test = null;
- $this->assertTrue(!isset($this->Controller->Test));
- $this->Controller->Common->loadComponent(array('RequestHandler', 'Test'=>array('x'=>'y')));
- $this->assertTrue(isset($this->Controller->Test));
- $this->assertTrue($this->Controller->Test->isInit);
- $this->assertTrue($this->Controller->Test->isStartup);
- }
- /**
- * CommonComponentTest::testLoadLib()
- *
- * @return void
- */
- public function testLoadLib() {
- $this->assertTrue(!isset($this->Controller->RandomLib));
- $this->Controller->Common->loadLib('Tools.RandomLib');
- $this->assertTrue(isset($this->Controller->RandomLib));
- $res = $this->Controller->RandomLib->pwd(null, 10);
- $this->assertTrue(!empty($res));
- # with options
- $this->assertTrue(!isset($this->Controller->TestLib));
- $this->Controller->Common->loadLib(array('Tools.RandomLib', 'TestLib'=>array('x'=>'y')));
- $this->assertTrue(isset($this->Controller->TestLib));
- $this->assertTrue($this->Controller->TestLib->hasOptions);
- }
- /**
- * CommonComponentTest::testGetParams()
- *
- * @return void
- */
- public function testGetParams() {
- if (php_sapi_name() !== 'cli') {
- $is = $this->Controller->Common->getQueryParam('case');
- $this->assertTrue(strpos($is, 'CommonComponent') > 0 || $is === 'AllComponentTests' || $is === 'AllTools');
- }
- $is = $this->Controller->Common->getQueryParam('x');
- $this->assertSame(null, $is);
- $is = $this->Controller->Common->getQueryParam('x', 'y');
- $this->assertSame($is, 'y');
- $is = $this->Controller->Common->getNamedParam('plugin');
- $this->assertSame(null, $is);
- $is = $this->Controller->Common->getNamedParam('x');
- $this->assertSame(null, $is);
- $is = $this->Controller->Common->getNamedParam('x', 'y');
- $this->assertSame($is, 'y');
- }
- /**
- * CommonComponentTest::testGetDefaultUrlParams()
- *
- * @return void
- */
- public function testGetDefaultUrlParams() {
- $is = $this->Controller->Common->defaultUrlParams();
- $this->assertNotEmpty($is);
- }
- /**
- * CommonComponentTest::testcurrentUrl()
- *
- * @return void
- */
- public function testCurrentUrl() {
- $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
- $is = $this->Controller->Common->currentUrl();
- $this->assertTrue(is_array($is) && !empty($is));
- $is = $this->Controller->Common->currentUrl(true);
- $this->assertTrue(!is_array($is) && !empty($is));
- }
- /**
- * CommonComponentTest::testIsForeignReferer()
- *
- * @return void
- */
- public function testIsForeignReferer() {
- $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
- $ref = 'http://www.spiegel.de';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertTrue($is);
- $ref = HTTP_BASE . '/some/controller/action';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertFalse($is);
- $ref = '';
- $is = $this->Controller->Common->isForeignReferer($ref);
- $this->assertFalse($is);
- }
- /**
- * CommonComponentTest::testTransientFlashMessage()
- *
- * @return void
- */
- public function testTransientFlashMessage() {
- $is = $this->Controller->Common->transientFlashMessage('xyz', 'success');
- //$this->assertTrue($is);
- $res = Configure::read('messages');
- //debug($res);
- $this->assertTrue(!empty($res));
- $this->assertTrue(isset($res['success'][0]) && $res['success'][0] === 'xyz');
- }
- /**
- * CommonComponentTest::testFlashMessage()
- *
- * @return void
- */
- public function testFlashMessage() {
- $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
- $this->Controller->Session->delete('messages');
- $is = $this->Controller->Common->flashMessage('efg');
- $res = $this->Controller->Session->read('messages');
- $this->assertTrue(!empty($res));
- $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
- }
- /**
- * CommonComponentTest::testManualLogin()
- *
- * @return void
- */
- public function testManualLogin() {
- $user = array(
- 'name' => 'foo',
- 'password' => 123,
- 'role_id' => 1,
- );
- $User = ClassRegistry::init('ToolsUser');
- $User->create();
- $res = $User->save($user);
- $this->assertTrue(!empty($res));
- $res = CakeSession::read('Auth');
- $this->assertNull($res);
- $is = $this->Controller->Common->manualLogin(2222);
- $this->assertFalse($is);
- $is = $this->Controller->Common->manualLogin($User->id);
- $this->assertTrue($is);
- $res = CakeSession::read('Auth');
- $this->assertSame($User->id, $res['User']['id']);
- $this->assertTrue(!empty($res['User']['Role']));
- }
- /**
- * CommonComponentTest::testForceLogin()
- *
- * @return void
- */
- public function testForceLogin() {
- $user = array(
- 'name' => 'foo',
- 'password' => 123,
- 'role_id' => 1,
- );
- $User = ClassRegistry::init('ToolsUser');
- $User->create();
- $res = $User->save($user);
- $this->assertTrue(!empty($res));
- $res = CakeSession::read('Auth');
- $this->assertNull($res);
- $is = $this->Controller->Common->forceLogin(2222);
- $this->assertFalse($is);
- $is = $this->Controller->Common->forceLogin($User->id);
- $this->assertTrue($is);
- $res = CakeSession::read('Auth');
- $this->assertSame($User->id, $res['User']['id']);
- $this->assertTrue(!empty($res['User']['Role']));
- }
- public function testGetGroup() {
- $list = array(
- 'Models' => array(
- '1' => 'Foo',
- '2' => 'Bar'
- ),
- 'Mitarbeiter' => array(
- '3' => 'Some',
- '4' => 'Thing'
- ),
- );
- $matching = array('Models' => 'Model', 'Mitarbeiter' => 'Contributor');
- $res = CommonComponent::getGroup($list, 111);
- $this->assertEquals('', $res);
- $res = CommonComponent::getGroup($list, 2);
- $this->assertEquals('Models', $res);
- $res = CommonComponent::getGroup($list, 2, $matching);
- $this->assertEquals('Model', $res);
- $res = CommonComponent::getGroup($list, 3, $matching);
- $this->assertEquals('Contributor', $res);
- }
- }
- /*** additional helper classes ***/
- class ToolsUser extends AppModel {
- public $name = 'ToolsUser';
- public $alias = 'User';
- public $belongsTo = array(
- 'Role',
- );
- }
- class CommonComponentTestController extends AppController {
- public $components = array('Tools.Common', 'Auth');
- public $failed = false;
- public $testHeaders = array();
- public function fail() {
- $this->failed = true;
- }
- public function redirect($url, $status = null, $exit = true) {
- return $status;
- }
- public function header($status) {
- $this->testHeaders[] = $status;
- }
- }
- class TestComponent extends Component {
- public $Controller;
- public $isInit = false;
- public $isStartup = false;
- public function initialize(Controller $Controller) {
- //$this->Controller = $Controller;
- $this->isInit = true;
- }
- public function startup(Controller $Controller) {
- //$this->Controller = $Controller;
- $this->isStartup = true;
- }
- }
- class TestHelper extends Object {
- }
- class TestLib {
- public $hasOptions = false;
- public function __construct($options = array()) {
- if (!empty($options)) {
- $this->hasOptions = true;
- }
- }
- }
|