public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
Added validate_numericality_of validator method
Changed add_validation_error to add_error for external uses.
add_validation_error becomes protected to handle a flag properly.
Changed a few admin pages to use the add_error method instead.
Almost done the ORM test coverage.


git-svn-id: http://svn.cmsmadesimple.org/svn/cmsmadesimple/trunk@4536 
3d254a34-79dc-0310-9e5f-be208747d8a0
tedkulp (author)
Wed May 28 05:18:04 -0700 2008
commit  758653aa659219fd0d4a27f2077ff088667e297c
tree    c233d323aea49e40b5f2366bae979ddca4f6aa0d
parent  732f687bf234822526bb48a8a1226160dcd9547d
...
56
57
58
59
 
60
61
62
63
64
 
65
66
67
...
56
57
58
 
59
60
61
62
63
 
64
65
66
67
0
@@ -56,12 +56,12 @@ function &get_user_object()
0
       else
0
       {
0
         //Add validation error about passwords not matching
0
- $user_object->add_validation_error(lang("Passwords don't match"));
0
+ $user_object->add_error(lang("Passwords don't match"));
0
       }
0
     }
0
     else
0
     {
0
- $user_object->add_validation_error(lang("No password given"));
0
+ $user_object->add_error(lang("No password given"));
0
     }
0
   }
0
   return $user_object;
...
56
57
58
59
 
60
61
62
...
56
57
58
 
59
60
61
62
0
@@ -56,7 +56,7 @@ function get_user_object($user_id)
0
       else
0
       {
0
         //Add validation error about passwords not matching
0
- $user_object->add_validation_error(lang("Passwords don't match"));
0
+ $user_object->add_error(lang("Passwords don't match"));
0
       }
0
     }
0
   }
...
49
50
51
52
 
53
54
55
...
49
50
51
 
52
53
54
55
0
@@ -49,7 +49,7 @@ class BlockHtml extends CmsObject
0
   {
0
     if ($page->get_property_value($block_name . '-content', $lang) == '')
0
     {
0
- $page->add_validation_error(lang('nofieldgiven', array(lang('content'))));
0
+ $page->add_error(lang('nofieldgiven', array(lang('content'))));
0
     }
0
   }
0
   
...
822
823
824
825
826
827
828
...
1391
1392
1393
1394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1395
1396
1397
1398
1399
1400
 
 
1401
1402
1403
1404
1405
1406
 
1407
1408
 
1409
1410
1411
1412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1413
1414
1415
...
822
823
824
 
825
826
827
...
1390
1391
1392
 
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
 
1420
1421
1422
1423
1424
1425
1426
 
1427
1428
 
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
0
@@ -822,7 +822,6 @@ abstract class CmsObjectRelationalMapping extends CmsObject implements ArrayAcce
0
           {
0
             if ($this->params[$localname] instanceof CmsDateTime)
0
             {
0
- //var_dump($this->params[$localname]);
0
               $midpart .= "{$table}.{$onefield} = " . $this->params[$localname]->to_sql_string() . ", ";
0
             }
0
             else
0
@@ -1391,25 +1390,62 @@ abstract class CmsObjectRelationalMapping extends CmsObject implements ArrayAcce
0
   {
0
     if ($this->$field == null || $this->$field == '')
0
     {
0
- $this->add_validation_error(($message != '' ? $message : lang('nofieldgiven',array($field))));
0
+ $this->add_validation_error(($message != '' ? $message : lang("%s must not be blank", $this->$field)));
0
+ }
0
+ }
0
+
0
+ /**
0
+ * Validation method to see if a parameter has been filled in. This should
0
+ * be called from an object's validate() method on each field that needs to be
0
+ * filled in before it can be saved.
0
+ *
0
+ * @param string Name of the field to check
0
+ * @param string If given, this is the message that will be set in the object if the method didn't succed.
0
+ * @return void
0
+ * @author Ted Kulp
0
+ */
0
+ function validate_numericality_of($field, $message = '')
0
+ {
0
+ if (!($this->$field == null || $this->field != ''))
0
+ {
0
+ if ((string)$this->$field != (string)intval($this->$field) && (string)$this->$field != (string)floatval($this->$field))
0
+ {
0
+ $this->add_validation_error(($message != '' ? $message : lang("%s must be a number", $this->$field)));
0
+ }
0
     }
0
   }
0
   
0
   /**
0
    * Method for quickly adding a new validation error to the object. If this is
0
- * called, then it's a safe bet that save() will fail.
0
+ * called, then it's a safe bet that save() will fail. This should only be
0
+ * used for setting validation errors from external sources.
0
    *
0
    * @param string Message to add to the validation error stack
0
    * @return void
0
    * @author Ted Kulp
0
    */
0
- function add_validation_error($message)
0
+ public function add_error($message)
0
   {
0
- $this->validation_errors[] = $message;
0
+ $this->add_validation_error($message);
0
     $this->clear_errors = false;
0
   }
0
   
0
   /**
0
+ * Method for quickly adding a new validation error to the object. If this is
0
+ * called, then it's a safe bet that save() will fail. This should only be
0
+ * used for setting validation errors from the object itself, as it doesn't
0
+ * set the clear_errors flag.
0
+ *
0
+ * @param string Message to add to the validation error stack
0
+ * @return void
0
+ * @author Ted Kulp
0
+ */
0
+ protected function add_validation_error($message)
0
+ {
0
+ $this->validation_errors[] = $message;
0
+ }
0
+
0
+ /**
0
    * Method to call the validation methods properly.
0
    *
0
    * @return int The number of validation errors
...
32
33
34
 
 
35
36
37
38
39
40
 
41
42
 
 
 
 
 
 
 
 
 
 
 
43
44
45
46
 
47
48
49
...
57
58
59
60
 
61
62
63
...
168
169
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
172
173
174
175
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
178
179
...
32
33
34
35
36
37
38
39
40
41
 
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
71
72
73
 
74
75
76
77
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
 
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
0
@@ -32,18 +32,32 @@ class CmsObjectRelationalMappingTest extends PHPUnit_Framework_TestCase
0
       id I KEY AUTO,
0
       test_field C(255),
0
       another_test_field C(255),
0
+ some_int I,
0
+ some_float F,
0
       create_date T,
0
       modified_date T
0
     ");
0
     
0
     $cms_db_prefix = CMS_DB_PREFIX;
0
- cms_db()->Execute("INSERT INTO {$cms_db_prefix}test_orm_table (test_field, another_test_field, create_date, modified_date) VALUES ('test', 'blah', now() - 10, now() - 10)");
0
+ cms_db()->Execute("INSERT INTO {$cms_db_prefix}test_orm_table (test_field, another_test_field, some_int, some_float, create_date, modified_date) VALUES ('test', 'blah', 5, 5.501, now() - 10, now() - 10)");
0
     cms_db()->Execute("INSERT INTO {$cms_db_prefix}test_orm_table (test_field, create_date, modified_date) VALUES ('test2', now(), now())");
0
     cms_db()->Execute("INSERT INTO {$cms_db_prefix}test_orm_table (test_field, create_date, modified_date) VALUES ('test3', now(), now())");
0
+
0
+ @CmsDatabase::drop_table('test_orm_table_child');
0
+ CmsDatabase::create_table('test_orm_table_child', "
0
+ id I KEY AUTO,
0
+ parent_id I,
0
+ some_other_field C(255),
0
+ create_date T,
0
+ modified_date T
0
+ ");
0
+
0
+ cms_db()->Execute("INSERT INTO {$cms_db_prefix}test_orm_table_child (parent_id, some_other_field, create_date, modified_date) VALUES (1, 'test', now(), now())");
0
   }
0
   
0
   public function tearDown()
0
   {
0
+ CmsDatabase::drop_table('test_orm_table_child');
0
     CmsDatabase::drop_table('test_orm_table');
0
     CmsCache::clear();
0
   }
0
@@ -57,7 +71,7 @@ class CmsObjectRelationalMappingTest extends PHPUnit_Framework_TestCase
0
   public function testGetColumnsInTableShouldWork()
0
   {
0
     $result = cms_orm('test_orm_table')->get_columns_in_table();
0
- $this->assertEquals(5, count($result));
0
+ $this->assertEquals(7, count($result));
0
     $this->assertEquals('int', $result['id']->type);
0
     $this->assertEquals('varchar', $result['test_field']->type);
0
     $this->assertEquals('datetime', $result['create_date']->type);
0
@@ -168,12 +182,89 @@ class CmsObjectRelationalMappingTest extends PHPUnit_Framework_TestCase
0
     $this->assertTrue($result->has_parameter('modified_date'));
0
     $this->assertFalse($result->has_parameter('i_made_this_up'));
0
   }
0
+
0
+ public function testValidatorWillNotAllowSaves()
0
+ {
0
+ $result = cms_orm('test_orm_table')->find();
0
+ $result->test_field = '';
0
+ $result->another_test_field = '';
0
+ $this->assertFalse($result->save());
0
+ $result->test_field = 'test';
0
+ $this->assertFalse($result->save());
0
+ $result->another_test_field = 'blah';
0
+ $this->assertTrue($result->save());
0
+ }
0
+
0
+ public function testNumericalityOfValidatorShouldActuallyWork()
0
+ {
0
+ $result = cms_orm('test_orm_table')->find();
0
+ $result->some_int = ''; #We're testing numbers, not empty strings -- do another validation
0
+ $this->assertTrue($result->save());
0
+ $result->some_int = '5';
0
+ $this->assertTrue($result->save());
0
+ $result->some_int = 5;
0
+ $this->assertTrue($result->save());
0
+ $result->some_float = 'sdfsdfsdfsfd';
0
+ $this->assertFalse($result->save());
0
+ $result->some_float = '5.501';
0
+ $this->assertTrue($result->save());
0
+ $result->some_float = 5.501;
0
+ $this->assertTrue($result->save());
0
+ }
0
+
0
+ public function testHasManyShouldWork()
0
+ {
0
+ $result = cms_orm('test_orm_table')->find_by_id(1);
0
+ $this->assertNotNull($result);
0
+ $this->assertEquals(1, count($result->children));
0
+ $this->assertEquals('test', $result->children[0]->some_other_field);
0
+ }
0
+
0
+ public function testBelongsToShouldWorkAsWell()
0
+ {
0
+ $result = cms_orm('test_orm_table')->find_by_id(1);
0
+ $this->assertNotNull($result);
0
+ $this->assertEquals(1, count($result->children));
0
+ $this->assertNotNull(count($result->children[0]->parent));
0
+ $this->assertEquals(1, $result->children[0]->parent->id);
0
+ }
0
+
0
+ public function testDeleteShouldActuallyDelete()
0
+ {
0
+ $result = cms_orm('test_orm_table')->find_by_id(1);
0
+ $this->assertNotNull($result);
0
+ $result->delete();
0
+ $result = cms_orm('test_orm_table')->find_all();
0
+ $this->assertEquals(2, count($result));
0
+ }
0
 
0
 }
0
 
0
 class TestOrmTable extends CmsObjectRelationalMapping
0
 {
0
-
0
+ public function setup()
0
+ {
0
+ $this->create_has_many_association('children', 'TestOrmTableChild', 'parent_id');
0
+ }
0
+
0
+ public function validate()
0
+ {
0
+ $this->validate_not_blank('test_field');
0
+ if (strlen($this->another_test_field) == 0)
0
+ {
0
+ $this->add_validation_error('can\'t be blank');
0
+ }
0
+ $this->validate_numericality_of('some_int');
0
+ $this->validate_numericality_of('some_float');
0
+ }
0
+}
0
+
0
+class TestOrmTableChild extends CmsObjectRelationalMapping
0
+{
0
+ public function setup()
0
+ {
0
+ $this->create_belongs_to_association('parent', 'test_orm_table', 'parent_id');
0
+ }
0
 }
0
 
0
 # vim:ts=4 sw=4 noet

Comments

    No one has commented yet.