ソースを参照

completeUrl link test and MyModel validateUnique tests

euromark 13 年 前
コミット
61e5ca4cca

+ 1 - 1
Console/Command/CodeShell.php

@@ -368,4 +368,4 @@ function strposReverse($str, $search) {
 
 	$posRev = strpos($str, $search);
 	return $posRev;
-}
+}

+ 44 - 1
Console/Command/WhitespaceShell.php

@@ -8,7 +8,13 @@ class WhitespaceShell extends AppShell {
 	# each report: [0] => found, [1] => corrected
 	public $report = array('leading'=>array(0, 0), 'trailing'=>array(0, 0));
 
+	/*
 	public function main() {
+
+	}
+	*/
+
+	public function find() {
 		$App = new Folder(APP);
 
 		$files = $App->findRecursive('.*\.php');
@@ -82,7 +88,10 @@ class WhitespaceShell extends AppShell {
 		$this->out('fixed '.$this->report['leading'][1].' leading, '.$this->report['trailing'][1].' trailing ws');
 	}
 
-	public function whitespaces() {
+	/**
+	 * whitespaces at the end of the file
+	 */
+	public function eof() {
 		if (!empty($this->args[0])) {
 			$folder = realpath($this->args[0]);
 		} else {
@@ -119,4 +128,38 @@ class WhitespaceShell extends AppShell {
 		}
 	}
 
+	public function getOptionParser() {
+		$subcommandParser = array(
+			'options' => array(
+				'ext' => array(
+					'short' => 'e',
+					'help' => __d('cake_console', 'Specify extensions [php|txt|...]'),
+					'default' => '',
+				),
+				'dry-run'=> array(
+					'short' => 'd',
+					'help' => __d('cake_console', 'Dry run the clear command, no files will actually be deleted. Should be combined with verbose!'),
+					'boolean' => true
+				),
+				'plugin'=> array(
+					'short' => 'p',
+					'help' => __d('cake_console', 'Plugin'),
+					'default' => '',
+				),
+			)
+		);
+
+		return parent::getOptionParser()
+			->description(__d('cake_console', 'The Whitespace Shell removes uncessary/wrong whitespaces.
+Either provide a path as first argument, use -p PluginName or run it as it is for the complete APP dir.'))
+			->addSubcommand('find', array(
+				'help' => __d('cake_console', 'Detect any leading/trailing whitespaces'),
+				'parser' => $subcommandParser
+			))
+			->addSubcommand('eof', array(
+				'help' => __d('cake_console', 'Fix whitespaces at the end of PHP files (a single newline as per coding standards)'),
+				'parser' => $subcommandParser
+			));
+	}
+
 }

+ 17 - 20
Model/MyModel.php

@@ -749,21 +749,13 @@ class MyModel extends Model {
 	 * TODO: fix it
 	 * TODO: rename it to just find() or integrate it there
 	 */
-	public function findNeighbors($type, $options = array()) {
-		if ($type == 'neighbors' && isset($options['scope'])) {
-			$type == 'neighborsTry';
-		}
-
-		switch ($type) {
-			case 'neighborsTry': # use own implementation
+	protected function _findNeighbors($state, $query, $results = array()) {
+		return parent::_findNeighbors($state, $query, $results);
 
-				return $xxx; # TODO: implement
-				break;
-
-			default:
-				return parent::find($type, $options);
-				break;
+		if (isset($query['scope'])) {
+			//TODO
 		}
+		return parent::find($type, $options);
 	}
 
 	/**
@@ -957,7 +949,7 @@ class MyModel extends Model {
 		}
 
 		$conditions = array($this->alias . '.' . $fieldName => $fieldValue, // Model.field => $this->data['Model']['field']
-			$this->alias . '.id !=' => $id, );
+			$this->alias . '.id !=' => $id);
 
 		# careful, if fields is not manually filled, the options will be the second param!!! big problem...
 		$fields = (array)$fields;
@@ -971,7 +963,7 @@ class MyModel extends Model {
 
 				} elseif (!empty($id)) {
 					# manual query! (only possible on edit)
-					$res = $this->find('first', array('fields' => array($this->alias.'.'.$dependingField), 'conditions' => array($this->alias.'.id' => $this->data[$this->alias]['id'])));
+					$res = $this->find('first', array('fields' => array($this->alias.'.'.$dependingField), 'conditions' => array($this->alias.'.id' => $id)));
 					if (!empty($res)) {
 						$conditions[$this->alias . '.' . $dependingField] = $res[$this->alias][$dependingField];
 					}
@@ -1607,24 +1599,29 @@ class MyModel extends Model {
 	 * from http://othy.wordpress.com/2006/06/03/generatenestedlist/
 	 * NEEDS parent_id
 	 * //TODO refactor for 1.2
+	 * @deprecated use generateTreeList instead
 	 * 2009-08-12 ms
 	 */
-	public function generateNestedList($conditions = null, $indent = '- - ') {
-		$cats = $this->find('threaded', array('conditions'=>$conditions, 'fields'=>array($this->alias.'.id', $this->alias.'.'.$this->displayField, $this->alias.'.parent_id')));
+	public function generateNestedList($conditions = null, $indent = '--') {
+		$cats = $this->find('threaded', array('conditions' => $conditions, 'fields' => array(
+				$this->name . '.id',
+				$this->name . '.name',
+				$this->name . '.parent_id')));
 		$glist = $this->_generateNestedList($cats, $indent);
 		return $glist;
 	}
 
 	/**
 	 * from http://othy.wordpress.com/2006/06/03/generatenestedlist/
-	 * @protected
+	 * @deprecated use generateTreeList instead
 	 * 2009-08-12 ms
 	 */
 	public function _generateNestedList($cats, $indent, $level = 0) {
 		static $list = array();
-		for ($i = 0, $c = count($cats); $i < $c; $i++) {
+		$c = count($cats);
+		for ($i = 0; $i < $c; $i++) {
 			$list[$cats[$i][$this->alias]['id']] = str_repeat($indent, $level) . $cats[$i][$this->alias][$this->displayField];
-			if (isset($cats[$i]['children']) && !empty($cats[$i]['children'])) {
+			if (!empty($cats[$i]['children'])) {
 				$this->_generateNestedList($cats[$i]['children'], $indent, $level + 1);
 			}
 		}

+ 45 - 0
Test/Case/Model/MyModelTest.php

@@ -562,6 +562,51 @@ class MyModelTest extends MyCakeTestCase {
 		$this->assertTrue($res);
 	}
 
+	public function testAppValidateUnique() {
+		$this->out($this->_header(__FUNCTION__));
+		//die(returns($this->Model->schema()));
+		$this->Model->validate['title'] = array(
+			'validateUnique' => array(
+				'rule' => 'validateUnique',
+				'message' => 'valErrRecordTitleExists'
+			),
+		);
+		$data = array(
+			'title' => 'abc',
+			'published' => 'N'
+		);
+		$this->Model->create($data);
+		$res = $this->Model->validates();
+		$this->assertTrue($res);
+		$res = $this->Model->save($res, false);
+		$this->assertTrue((bool)$res);
+
+		$this->Model->create();
+		$res = $this->Model->save($data);
+		$this->assertFalse($res);
+
+
+		$this->Model->validate['title'] = array(
+			'validateUnique' => array(
+				'rule' => array('validateUnique', array('published')),
+				'message' => 'valErrRecordTitleExists'
+			),
+		);
+		$data = array(
+			'title' => 'abc',
+			'published' => 'Y'
+		);
+		$this->Model->create($data);
+		$res = $this->Model->validates();
+		$this->assertTrue($res);
+		$res = $this->Model->save($res, false);
+		$this->assertTrue((bool)$res);
+
+		$this->Model->create();
+		$res = $this->Model->save($data);
+		$this->assertFalse($res);
+	}
+
 }
 
 class Post extends MyModel {

+ 31 - 1
View/Helper/MyHelper.php

@@ -176,12 +176,42 @@ class MyHelper extends Helper {
 	*/
 
 	/**
+	 * keep named and query params for pagination/filter after edit etc
+	 *
+	 * @params same as Html::link($title, $url, $options, $confirmMessage)
+	 * @return string Link
+	 * 2012-12-03 ms
+	 */
+	public function completeLink($title, $url = null, $options = array(), $confirmMessage = false) {
+		if (is_array($url)) {
+			$url += $this->params['named'];
+		}
+		return $this->link($title, $url, $options, $confirmMessage);
+	}
+
+	/**
+	 * keep named and query params for pagination/filter after edit etc
+	 *
+	 * @params same as Html::url($url, $options, $escape)
+	 * @return string Link
+	 * 2012-12-03 ms
+	 */
+	public function completeUrl($url = null, $full = false, $escape = true) {
+		if (is_array($url)) {
+			$url += $this->params['named'];
+		}
+		return $this->url($url, $options, $escape);
+	}
+
+	/**
 	 * convenience function for normal links
 	 * useful for layout links and links inside elements etc
+	 *
 	 * @params same as Html::link($title, $url, $options, $confirmMessage)
+	 * @return string Link
 	 * 2010-01-23 ms
 	 */
-	public function defaultLink($title, $url=null, $options=array(), $confirmMessage=false) {
+	public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
 		if ($this->linkDefaults === null) {
 			if (!class_exists('CommonComponent')) {
 				App::import('Component', 'Tools.Common');