Browse Source

2.0first classes

m 14 years ago
parent
commit
eae49ccd4b

+ 67 - 0
Test/Case/Behaviors/CaptchaBehaviorTest.php

@@ -0,0 +1,67 @@
+<?php
+
+App::uses('CaptchaBehavior', 'Tools.Model/Behavior');
+App::uses('MyCakeTestCase', 'Tools.Lib');
+
+class CaptchaBehaviorTest extends MyCakeTestCase {
+
+	public $fixtures = array(
+		'core.comment'
+	);
+	
+	public $Comment;
+
+	public function startTest() {
+		
+	}
+
+	public function setUp() {
+		$this->Comment = ClassRegistry::init('Comment');
+		$this->Comment->Behaviors->attach('Tools.Captcha', array());
+	}
+
+	public function tearDown() {
+		unset($this->Comment);
+	}
+
+	/**
+	 * test if nothing has been
+	 */
+	public function testEmpty() {
+		$is = $this->Comment->validates();
+		debug($this->Comment->invalidFields());
+		$this->assertFalse($is);
+	}
+
+	public function testWrong() {
+		$data = array('title'=>'xyz', 'captcha'=>'x', 'captcha_hash'=>'y', 'captcha_time'=>'123');
+		$this->Comment->set($data);
+		$is = $this->Comment->validates();
+		debug($this->Comment->invalidFields());
+		$this->assertFalse($is);
+		
+		$data = array('title'=>'xyz', 'captcha'=>'x', 'homepage'=>'', 'captcha_hash'=>'y', 'captcha_time'=>'123');
+		$this->Comment->set($data);
+		$is = $this->Comment->validates();
+		debug($this->Comment->invalidFields());
+		$this->assertFalse($is);
+	}
+	
+	public function testCorrect() {
+		App::import('Lib', 'Tools.CaptchaLib');
+		$Captcha = new CaptchaLib();
+		$hash = $Captcha->buildHash(array('captcha'=>2, 'captcha_time'=>time()-10, ''), CaptchaLib::$defaults);
+		
+		$data = array('title'=>'xyz', 'captcha'=>'2', 'homepage'=>'', 'captcha_hash'=>$hash, 'captcha_time'=>time()-10);
+		$this->Comment->set($data);
+		$is = $this->Comment->validates();
+		debug($this->Comment->invalidFields());
+		$this->assertTrue($is);
+		
+	}
+
+	//TODO
+
+}
+
+

+ 258 - 0
Test/Case/Behaviors/ChangePasswordBehaviorTest.php

@@ -0,0 +1,258 @@
+<?php
+
+App::uses('Model', 'Model');
+App::uses('AppModel', 'Model');
+/*
+class User extends AppModel {
+	
+	public $useDbConfig = 'test';
+	public $cacheSources = false;
+	
+	public function invalidFields($x) {
+		$res = parent::invalidFields($x);
+		return $res;
+	}
+	
+	
+}
+*/
+
+class ChangePasswordBehaviorTest extends CakeTestCase {
+
+	public $fixtures = array(
+		'core.user',
+	);
+	
+/**
+ * setUp method
+ */
+	public function setUp() {
+		//$this->loadFixtures('User');
+		$this->User = ClassRegistry::init('User');
+	}
+
+/**
+ * Tear-down method.  Resets environment state.
+ */
+	public function tearDown() {
+		$this->User->Behaviors->detach('ChangePassword');
+		unset($this->User);
+	}
+
+
+	public function testObject() {
+		$this->User->Behaviors->attach('Tools.ChangePassword', array());
+		$this->assertIsA($this->User->Behaviors->ChangePassword, 'ChangePasswordBehavior');
+		$res = $this->User->Behaviors->attached('ChangePassword');
+		$this->assertTrue($res);
+	}
+	
+	public function testValidate() {
+		$this->User->Behaviors->attach('Tools.ChangePassword', array());
+		
+		
+		$this->User->create();
+		$data = array(
+			'pwd' => '1234',
+		);
+		$this->User->set($data);
+		//debug($this->User->data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());	
+		//debug($this->User->validate);
+		$this->assertFalse($is);
+		
+		
+		$this->User->create();
+		$data = array(
+			'pwd' => '1234',
+			'pwd_repeat' => '123456'
+		);
+		$this->User->set($data);
+		//debug($this->User->data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());	
+		//debug($this->User->validate);
+		$this->assertFalse($is);
+		
+
+		
+		$this->User->create();
+		$data = array(
+			'pwd' => '123456',
+			'pwd_repeat' => '123456'
+		);
+		$this->User->set($data);
+		//debug($this->User->validate);
+		$is = $this->User->validates();
+		$this->assertTrue(!empty($is));
+		
+	}
+
+	/**
+	 * needs faking of pwd check...
+	 */
+	public function testValidateCurrent() {
+		$this->User->create();
+		$data = array('username'=>'xyz', 'password'=>Security::hash('some', null, true));
+		$res = $this->User->save($data);
+		$uid = $this->User->id;
+		debug($res);
+		
+		//App::import('Component', 'Tools.AuthExt');
+		
+		$this->User->Behaviors->attach('Tools.ChangePassword', array('current'=>true));
+		$this->User->create();
+		$data = array(
+			'id' => $uid,
+			'pwd' => '1234',
+			'pwd_repeat' => '123456'
+		);
+		$this->User->set($data);
+		//debug($this->User->data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());	
+		//debug($this->User->validate);
+		$this->assertFalse($is);
+		
+		$this->User->create();
+		$data = array(
+			'id' => $uid,
+			'pwd_current' => 'somex',
+			'pwd' => '123456',
+			'pwd_repeat' => '123456'
+		);
+		$this->User->set($data);
+		debug($this->User->invalidFields());	
+		$is = $this->User->save();
+		$this->assertFalse($is);
+		
+		$this->User->create();
+		$data = array(
+			'id' => $uid,
+			'pwd_current' => 'some',
+			'pwd' => '123456',
+			'pwd_repeat' => '123456'
+		);
+		$this->User->set($data);
+		debug($this->User->invalidFields());	
+		$is = $this->User->save();
+		$this->assertTrue(!empty($is));
+	}
+	
+	public function testValidateNoConfirm() {
+		$this->User->Behaviors->attach('Tools.ChangePassword', array('confirm'=>false));
+		$this->User->create();
+		$data = array(
+			'pwd' => '123456',
+		);
+		$this->User->set($data);
+		$is = $this->User->save();
+		debug($is);
+		$this->assertTrue(!empty($is));
+	}
+	
+	public function testValidateNonEmptyToEmpty() {
+		$this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>false));
+		$this->User->create();
+		$data = array(
+			'pwd' => '',
+			'pwd_repeat' => ''
+		);
+		$this->User->set($data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());
+		debug($is);
+		$this->assertFalse($is);	
+		
+		//TODO:
+		$this->User->Behaviors->detach('ChangePassword');
+		
+		$this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>true));
+		$this->User->create();
+		$data = array(
+			'pwd' => '',
+			'pwd_repeat' => ''
+		);
+		$this->User->set($data);
+		//debug($this->User->data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());	
+		$this->assertFalse($is);	
+	}
+	
+	public function testDifferentFieldNames() {		
+		$this->User->Behaviors->attach('Tools.ChangePassword', array(
+			'formField' => 'passw',
+			'formFieldRepeat' => 'passw_repeat',
+			'formFieldCurrent' => 'passw_current',
+		));
+		$this->User->create();
+		$data = array(
+			'passw' => '123456',
+			'passw_repeat' => '123456'
+		);
+		$this->User->set($data);
+		//debug($this->User->data);
+		$is = $this->User->save();
+		$this->assertTrue(!empty($is));
+		
+	}
+	
+	public function testNotSame() {		
+		$this->User->Behaviors->attach('Tools.ChangePassword', array(
+			'formField' => 'passw',
+			'formFieldRepeat' => 'passw_repeat',
+			'formFieldCurrent' => 'passw_current',
+			'allowSame' => false,
+			'current' => true
+		));
+		$this->User->create();
+		$data = array(
+			'id' => 5,
+			'passw_current' => 'some',
+			'passw' => 'some',
+			'passw_repeat' => 'some'
+		);
+		$this->User->set($data);
+		$is = $this->User->save();
+		debug($this->User->invalidFields());	
+		$this->assertFalse($is);
+		
+		$this->User->create();
+		$data = array(
+			'id' => 5,
+			'passw_current' => 'some',
+			'passw' => 'new',
+			'passw_repeat' => 'new'
+		);
+		$this->User->set($data);
+		debug($this->User->data);
+		$is = $this->User->save();
+		$this->assertTrue(!empty($is));
+	}
+	
+}
+
+
+/**
+ * FAKER!
+ * 2011-11-03 ms
+ */
+class AuthComponent {
+	
+	
+	public function constructAuthenticate() {
+		
+	}
+	
+	public function verifyUser($user, $pwd) {
+		if ($user == '5' && $pwd == 'some') {
+			return true;
+		}
+		return false;
+	}
+	
+}
+
+

+ 185 - 0
Test/Case/Behaviors/JsonableBehaviorTest.php

@@ -0,0 +1,185 @@
+<?php
+
+App::import('Behavior', 'Tools.Jsonable');
+App::import('Model', 'App');
+App::uses('MyCakeTestCase', 'Tools.Lib');
+
+class JsonableTestModel extends AppModel {
+
+	public $useTable = false;
+	public $displayField = 'title';
+
+	public function find($type = null, $options = array(), $customData = null) {
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => '{"x":"y"}',
+		);
+		if ($customData !== null) {
+			$data = $customData;
+		}
+		if ($type == 'count') {
+			$results = array(0=>array(0=>array('count'=>2)));
+		} else {
+			$results = array(0=>array($this->alias=>$data));
+		}
+
+		$results = $this->_filterResults($results);
+		if ($type == 'first') {
+			$results = $this->_findFirst('after', null, $results);
+		}
+		return $results;
+	}
+}
+
+class JsonableTest extends MyCakeTestCase {
+	/*
+	public $fixtures = array(
+		'core.comment'
+	);
+	*/
+	public $Comment;
+
+	public function startTest() {
+		//$this->Comment = ClassRegistry::init('Comment');
+		$this->Comment = new JsonableTestModel();
+		$this->Comment->Behaviors->attach('Jsonable', array());
+	}
+
+/** INPUT **/
+
+	public function testBasic() {
+		echo $this->_header(__FUNCTION__);
+		// accuracy >= 5
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => array('x'=>'y'),
+		);
+		$res = $this->Comment->save($data);
+		$this->assertTrue($res);
+
+		$res = $this->Comment->data;
+		echo returns($res);
+		$this->assertIdentical($res['JsonableTestModel']['details'], '{"x":"y"}');
+	}
+
+	public function testFieldsWithList() {
+		echo $this->_header(__FUNCTION__);
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'list'));
+
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => 'z|y|x',
+		);
+		$res = $this->Comment->save($data);
+		$this->assertTrue($res);
+
+		$res = $this->Comment->data;
+		echo returns($res);
+		$this->assertIdentical($res['JsonableTestModel']['details'], '["z","y","x"]');
+
+		# with sort and unique
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => 'z|x|y|x',
+		);
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'list', 'sort'=>true));
+
+		$res = $this->Comment->save($data);
+		$this->assertTrue($res);
+
+		$res = $this->Comment->data;
+		echo returns($res);
+		$this->assertIdentical($res['JsonableTestModel']['details'], '["x","y","z"]');
+	}
+
+	public function testFieldsWithParam() {
+		echo $this->_header(__FUNCTION__);
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details'), 'input'=>'param'));
+
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => 'z:vz|y:yz|x:xz',
+		);
+		$res = $this->Comment->save($data);
+		$this->assertTrue($res);
+
+		$res = $this->Comment->data;
+		echo returns($res);
+		$this->assertIdentical($res['JsonableTestModel']['details'], '{"z":"vz","y":"yz","x":"xz"}');
+	}
+
+
+
+/** OUTPUT **/
+
+	public function testFieldsOnFind() {
+		echo $this->_header(__FUNCTION__);
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('fields'=>array('details')));
+
+		$res = $this->Comment->find('first', array());
+
+		$this->assertEqual($res['JsonableTestModel']['details'], array('x'=>'y'));
+		pr($res);
+
+
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('output'=>'param', 'fields'=>array('details')));
+
+		$res = $this->Comment->find('first', array());
+		pr($res);
+		$this->assertEqual($res['JsonableTestModel']['details'], 'x:y');
+
+
+
+
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('output'=>'list', 'fields'=>array('details')));
+
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => '["z","y","x"]',
+		);
+		$res = $this->Comment->find('first', array(), $data);
+		pr($res);
+		$this->assertEqual($res['JsonableTestModel']['details'], 'z|y|x');
+
+		echo BR.BR;
+
+
+		$this->Comment->Behaviors->detach('Jsonable');
+		$this->Comment->Behaviors->attach('Jsonable', array('output'=>'list', 'separator'=>', ', 'fields'=>array('details')));
+
+		$data = array(
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'name' => 'some Name',
+			'details' => '["z","y","x"]',
+		);
+		$res = $this->Comment->find('first', array(), $data);
+		pr($res);
+		$this->assertEqual($res['JsonableTestModel']['details'], 'z, y, x');
+
+
+		echo BR.BR;
+
+		$res = $this->Comment->find('all', array(), $data);
+		pr($res);
+		$this->assertEqual($res[0]['JsonableTestModel']['details'], 'z, y, x');
+	}
+}