Browse Source

Fixes and cleanup

euromark 12 years ago
parent
commit
e4e465d60a

+ 2 - 0
Controller/Component/Auth/DirectAuthenticate.php

@@ -18,6 +18,8 @@ App::uses('BaseAuthenticate', 'Controller/Component/Auth');
  *   and its common _findUser() method). It also respects the scope and contain settings specified
  *   in your AppController just as any other adapter.
  *
+ * Possible configs: see BaseAuthenticate.
+ *
  * @author Mark Scherer
  * @license MIT
  * @cakephp 2.x (>= 2.3)

File diff suppressed because it is too large
+ 216 - 391
Controller/Component/CommonComponent.php


+ 173 - 73
Test/Case/Controller/Component/CommonComponentTest.php

@@ -3,34 +3,24 @@
 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');
+	public $fixtures = array('core.cake_session', 'plugin.tools.user', 'plugin.tools.role');
 
-/**
- * setUp method
- *
- * @access public
- * @return void
- */
 	public function setUp() {
 		parent::setUp();
+		CakeSession::delete('Auth');
 
 		$this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
 		$this->Controller->constructClasses();
 		$this->Controller->startupProcess();
 	}
 
-/**
- * Tear-down method. Resets environment state.
- *
- * @access public
- * @return void
- */
 	public function tearDown() {
 		parent::tearDown();
 
@@ -38,15 +28,22 @@ class CommonComponentTest extends CakeTestCase {
 		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');
@@ -67,6 +64,11 @@ class CommonComponentTest extends CakeTestCase {
 		$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');
@@ -82,6 +84,11 @@ class CommonComponentTest extends CakeTestCase {
 		$this->assertTrue($this->Controller->TestLib->hasOptions);
 	}
 
+	/**
+	 * CommonComponentTest::testGetParams()
+	 *
+	 * @return void
+	 */
 	public function testGetParams() {
 		if (php_sapi_name() !== 'cli') {
 			$is = $this->Controller->Common->getQueryParam('case');
@@ -104,12 +111,57 @@ class CommonComponentTest extends CakeTestCase {
 		$this->assertSame($is, 'y');
 	}
 
+	/**
+	 * CommonComponentTest::testGetDefaultUrlParams()
+	 *
+	 * @return void
+	 */
 	public function testGetDefaultUrlParams() {
 		$is = $this->Controller->Common->defaultUrlParams();
-		//debug($is);
 		$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);
@@ -120,6 +172,11 @@ class CommonComponentTest extends CakeTestCase {
 		$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');
 
@@ -131,82 +188,127 @@ class CommonComponentTest extends CakeTestCase {
 		$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 = 'User';
+
+	public $alias = 'User';
+
+	public $belongsTo = array(
+		'Role',
+	);
+
+}
 
-/**
-* Short description for class.
-*
-* @package cake.tests
-* @subpackage cake.tests.cases.libs.controller.components
-*/
 class CommonComponentTestController extends AppController {
-/**
- * name property
- *
- * @var string 'SecurityTest'
- * @access public
- */
 
-/**
- * components property
- *
- * @var array
- * @access public
- */
 	public $components = array('Tools.Common');
-/**
- * failed property
- *
- * @var bool false
- * @access public
- */
+
 	public $failed = false;
-/**
- * Used for keeping track of headers in test
- *
- * @var array
- * @access public
- */
+
 	public $testHeaders = array();
-/**
- * fail method
- *
- * @access public
- * @return void
- */
+
 	public function fail() {
 		$this->failed = true;
 	}
-/**
- * redirect method
- *
- * @param mixed $option
- * @param mixed $code
- * @param mixed $exit
- * @access public
- * @return void
- */
+
 	public function redirect($url, $status = null, $exit = true) {
 		return $status;
 	}
-/**
- * Conveinence method for header()
- *
- * @param string $status
- * @return void
- * @access public
- */
+
 	public function header($status) {
 		$this->testHeaders[] = $status;
 	}
 }
 
-
 class TestComponent extends Component {
 
 	public $Controller;
@@ -226,8 +328,6 @@ class TestComponent extends Component {
 }
 
 class TestHelper extends Object {
-
-
 }
 
 class TestLib {

+ 11 - 11
Test/Fixture/RoleFixture.php

@@ -1,5 +1,4 @@
 <?php
-/* Role Fixture generated on: 2011-11-20 21:59:33 : 1321822773 */
 
 /**
  * RoleFixture
@@ -7,11 +6,11 @@
  */
 class RoleFixture extends CakeTestFixture {
 
-/**
- * Fields
- *
- * @var array
- */
+	/**
+	 * Fields
+	 *
+	 * @var array
+	 */
 	public $fields = array(
 		'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary', 'collate' => NULL, 'comment' => ''),
 		'name' => array('type' => 'string', 'null' => false, 'length' => 64, 'collate' => 'utf8_unicode_ci', 'comment' => '', 'charset' => 'utf8'),
@@ -26,11 +25,11 @@ class RoleFixture extends CakeTestFixture {
 		'tableParameters' => array()
 	);
 
-/**
- * Records
- *
- * @var array
- */
+	/**
+	 * Records
+	 *
+	 * @var array
+	 */
 	public $records = array(
 		array(
 			'id' => '2',
@@ -77,4 +76,5 @@ class RoleFixture extends CakeTestFixture {
 			'active' => 1
 		),
 	);
+
 }