Skip to content
This repository
Browse code

Refactoring out Model.fk parsing. Updating create(). Adding tests.

  • Loading branch information...
commit 449c390d48e9d3a85a84d4f192850f95222f7188 1 parent 3aa6d22
Mark Story authored August 01, 2009
51  cake/console/libs/acl.php
@@ -137,25 +137,16 @@ function main() {
137 137
  * @access public
138 138
  */
139 139
 	function create() {
140  
-
141 140
 		$this->_checkArgs(3, 'create');
142 141
 		$this->checkNodeType();
143 142
 		extract($this->__dataVars());
144 143
 
145 144
 		$class = ucfirst($this->args[0]);
146  
-		$object = new $class();
147  
-
148  
-		if (preg_match('/^([\w]+)\.(.*)$/', $this->args[1], $matches) && count($matches) == 3) {
149  
-			$parent = array(
150  
-				'model' => $matches[1],
151  
-				'foreign_key' => $matches[2],
152  
-			);
153  
-		} else {
154  
-			$parent = $this->args[1];
155  
-		}
  145
+		$object = ClassRegistry::init($class);
  146
+		$parent = $this->parseIdentifier($this->args[1]);
156 147
 
157 148
 		if (!empty($parent) && $parent != '/' && $parent != 'root') {
158  
-			@$parent = $object->node($parent);
  149
+			$parent = $object->node($parent);
159 150
 			if (empty($parent)) {
160 151
 				$this->err(sprintf(__('Could not find parent node using reference "%s"', true), $this->args[1]));
161 152
 				return;
@@ -166,22 +157,15 @@ function create() {
166 157
 			$parent = null;
167 158
 		}
168 159
 
169  
-		if (preg_match('/^([\w]+)\.(.*)$/', $this->args[2], $matches) && count($matches) == 3) {
170  
-			$data = array(
171  
-				'model' => $matches[1],
172  
-				'foreign_key' => $matches[2],
173  
-			);
174  
-		} else {
175  
-			if (!($this->args[2] == '/')) {
176  
-				$data = array('alias' => $this->args[2]);
177  
-			} else {
178  
-				$this->error(__('/ can not be used as an alias!', true), __('\t/ is the root, please supply a sub alias', true));
179  
-			}
  160
+		$data = $this->parseIdentifier($this->args[2]);
  161
+		if (is_string($data) && !$data == '/') {
  162
+			$data = array('alias' => $data);
  163
+		} else if (is_string($data)) {
  164
+			$this->error(__('/ can not be used as an alias!', true), __('\t/ is the root, please supply a sub alias', true));
180 165
 		}
181 166
 
182 167
 		$data['parent_id'] = $parent;
183 168
 		$object->create();
184  
-
185 169
 		if ($object->save($data)) {
186 170
 			$this->out(sprintf(__("New %s '%s' created.\n", true), $class, $this->args[2]), true);
187 171
 		} else {
@@ -487,6 +471,23 @@ function nodeExists() {
487 471
 	}
488 472
 
489 473
 /**
  474
+ * Parse an identifier into Model.foriegnKey or an alias.
  475
+ * Takes an identifier determines its type and returns the result as used by other methods.
  476
+ *
  477
+ * @param string $identifier Identifier to parse
  478
+ * @return mixed a string for aliases, and an array for model.foreignKey
  479
+ **/
  480
+	function parseIdentifier($identifier) {
  481
+		if (preg_match('/^([\w]+)\.(.*)$/', $identifier, $matches)) {
  482
+			return array(
  483
+				'model' => $matches[1],
  484
+				'foreign_key' => $matches[2],
  485
+			);
  486
+		}
  487
+		return $identifier;
  488
+	}
  489
+
  490
+/**
490 491
  * get params for standard Acl methods
491 492
  *
492 493
  * @return array aro, aco, action
@@ -533,7 +534,7 @@ function __dataVars($type = null) {
533 534
 		}
534 535
 		$vars = array();
535 536
 		$class = ucwords($type);
536  
-		$vars['secondary_id'] = ife(strtolower($class) == 'aro', 'foreign_key', 'object_id');
  537
+		$vars['secondary_id'] = (strtolower($class) == 'aro') ? 'foreign_key' : 'object_id';
537 538
 		$vars['data_name'] = $type;
538 539
 		$vars['table_name'] = $type . 's';
539 540
 		$vars['class'] = $class;
50  cake/tests/cases/console/libs/acl.test.php
@@ -49,6 +49,8 @@
49 49
 	array('in', 'out', 'hr', 'createFile')
50 50
 );
51 51
 
  52
+Mock::generate('AclComponent', 'MockAclShellAclComponent');
  53
+
52 54
 /**
53 55
  * AclShellTest class
54 56
  *
@@ -82,7 +84,7 @@ function endCase() {
82 84
 	}
83 85
 
84 86
 /**
85  
- * setUp method
  87
+ * startTest method
86 88
  *
87 89
  * @return void
88 90
  * @access public
@@ -95,7 +97,7 @@ function startTest() {
95 97
 	}
96 98
 
97 99
 /**
98  
- * tearDown method
  100
+ * endTest method
99 101
  *
100 102
  * @return void
101 103
  * @access public
@@ -128,5 +130,49 @@ function testViewWithModelForeignKeyOutput() {
128 130
 
129 131
 		$this->Task->view();
130 132
 	}
  133
+/**
  134
+ * test the method that splits model.foreign key. and that it returns an array.
  135
+ *
  136
+ * @return void
  137
+ **/
  138
+	function testParsingModelAndForeignKey() {
  139
+		$result = $this->Task->parseIdentifier('Model.foreignKey');
  140
+		$expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
  141
+
  142
+		$result = $this->Task->parseIdentifier('mySuperUser');
  143
+		$this->assertEqual($result, 'mySuperUser');
  144
+
  145
+		$result = $this->Task->parseIdentifier('111234');
  146
+		$this->assertEqual($result, '111234');
  147
+	}
  148
+
  149
+/**
  150
+ * test creating aro/aco nodes
  151
+ *
  152
+ * @return void
  153
+ **/
  154
+	function testCreate() {
  155
+		$this->Task->args = array('aro', 'root', 'User.1');
  156
+		$this->Task->expectAt(0, 'out', array(new PatternExpectation('/created/'), '*'));
  157
+		$this->Task->create();
  158
+
  159
+		$Aro =& ClassRegistry::init('Aro');
  160
+		$Aro->cacheQueries = false;
  161
+		$result = $Aro->read();
  162
+		$this->assertEqual($result['Aro']['model'], 'User');
  163
+		$this->assertEqual($result['Aro']['foreign_key'], 1);
  164
+		$this->assertEqual($result['Aro']['parent_id'], null);
  165
+		$id = $result['Aro']['id'];
  166
+
  167
+		$this->Task->args = array('aro', 'User.1', 'User.3');
  168
+		$this->Task->expectAt(1, 'out', array(new PatternExpectation('/created/'), '*'));
  169
+		$this->Task->create();
  170
+
  171
+		$Aro =& ClassRegistry::init('Aro');
  172
+		$result = $Aro->read();
  173
+		$this->assertEqual($result['Aro']['model'], 'User');
  174
+		$this->assertEqual($result['Aro']['foreign_key'], 3);
  175
+		$this->assertEqual($result['Aro']['parent_id'], $id);
  176
+	}
131 177
 }
132 178
 ?>

0 notes on commit 449c390

Please sign in to comment.
Something went wrong with that request. Please try again.