Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

finished unit tests

  • Loading branch information...
commit f4d52ffec0f9fe5f5fc50264ebef0f5602a52214 1 parent 722cb7a
@mcurry authored
View
10 README
@@ -64,6 +64,10 @@
//do some validation here and return either true or false.
}
-Tips
- * I wrote an article for PHPArch about JavaScript validation(http://c7y.phparch.com/c/entry/1/art,improved_javascript_validation), which raised some concerns that this approach may reveal too much about an application's security. If this is a concern for you, but you still want to use this helper, there is an option to whitelist rules can be applied on the client side. To use to this feature set the list in your bootstrap.php:
- Configure::write('javascriptValidationWhitelist', array('alphaNumeric', 'minLength'));
+/* Tests */
+ 1. CakePHP unit tests are available through the normal /test.php
+ 2. JavaScript unit test can be run by visiting /js_validate/test.
+
+/* Tips */
+ * I wrote an article for PHPArch about JavaScript validation(http://c7y.phparch.com/c/entry/1/art,improved_javascript_validation), which raised some concerns that this approach may reveal too much about an application's security. If this is a concern for you, but you still want to use this helper, there is an option to whitelist rules can be applied on the client side. To use to this feature set the list in your bootstrap.php:
+ Configure::write('javascriptValidationWhitelist', array('alphaNumeric', 'minLength'));
View
18 models/js_validate.php
@@ -64,6 +64,24 @@ class JsValidate extends JsValidateAppModel {
'numeric' => array(
'rule1' => array('rule' => 'numeric')
),
+ 'notEmpty' => array(
+ 'rule1' => array('rule' => 'notEmpty')
+ ),
+ 'phone' => array(
+ 'rule1' => array('rule' => array('phone', null, 'us'))
+ ),
+ 'postal' => array(
+ 'rule1' => array('rule' => array('postal', null, 'us'))
+ ),
+ 'range' => array(
+ 'rule1' => array('rule' => array('range', 0, 10))
+ ),
+ 'ssn' => array(
+ 'rule1' => array('rule' => array('ssn', null, 'us'))
+ ),
+ 'url' => array(
+ 'rule1' => array('rule' => 'url')
+ )
);
}
?>
View
39 tests/cases/models/js_validate.test.php
@@ -161,10 +161,45 @@ function testNumeric() {
$this->assertEqual($expected, $validation);
}
- function atestNotEmpty() {
+ function testNotEmpty() {
$this->JsValidate->validate = array('notEmpty' => $this->JsValidate->backupValidate['notEmpty']);
$validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
- $expected = '{"JsValidate.JsValidateNotEmpty":[{"rule":"\/[^\\\\s]+\/m","message":"There was a problem with the field notEmpty"}]}';
+ $expected = '{"JsValidate.JsValidateNotEmpty":[{"rule":"\/[^\\\\s]+\/m","message":"There was a problem with the field NotEmpty"}]}';
+ $this->assertEqual($expected, $validation);
+ }
+
+ function testPhone() {
+ $this->JsValidate->validate = array('phone' => $this->JsValidate->backupValidate['phone']);
+ $validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
+ $expected = '{"JsValidate.JsValidatePhone":[{"rule":"\/^(?:\\\\+?1)?[-. ]?\\\\(?[2-9][0-8][0-9]\\\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$\/","message":"There was a problem with the field Phone"}]}';
+ $this->assertEqual($expected, $validation);
+ }
+
+ function testPostal() {
+ $this->JsValidate->validate = array('postal' => $this->JsValidate->backupValidate['postal']);
+ $validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
+ $expected = '{"JsValidate.JsValidatePostal":[{"rule":"\/^[0-9]{5}(?:-[0-9]{4})?$\/i","message":"There was a problem with the field Postal"}]}';
+ $this->assertEqual($expected, $validation);
+ }
+
+ function testRange() {
+ $this->JsValidate->validate = array('range' => $this->JsValidate->backupValidate['range']);
+ $validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
+ $expected = '{"JsValidate.JsValidateRange":[{"rule":{"rule":"range","params":[0,10]},"message":"There was a problem with the field Range"}]}';
+ $this->assertEqual($expected, $validation);
+ }
+
+ function testSsn() {
+ $this->JsValidate->validate = array('ssn' => $this->JsValidate->backupValidate['ssn']);
+ $validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
+ $expected = '{"JsValidate.JsValidateSsn":[{"rule":"\/^[0-9]{3}-[0-9]{2}-[0-9]{4}$\/i","message":"There was a problem with the field Ssn"}]}';
+ $this->assertEqual($expected, $validation);
+ }
+
+ function testUrl() {
+ $this->JsValidate->validate = array('url' => $this->JsValidate->backupValidate['url']);
+ $validation = $this->Validation->bind('JsValidate.JsValidate', array('form' => false));
+ $expected = '{"JsValidate.JsValidateUrl":[{"rule":"\/^(?:(?:https?|ftps?|file|news|gopher):\\\\\/\\\\\/)?(?:(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\\\\.){3}(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])|(?:[a-z0-9][-a-z0-9]*\\\\.)*(?:[a-z0-9][-a-z0-9]{0,62})\\\\.(?:(?:[a-z]{2}\\\\.)?[a-z]{2,4}|museum|travel))(?::[1-9][0-9]{0,3})?(?:\\\\\/?|\\\\\/([\\\\!\"\\\\$&\'\\\\(\\\\)\\\\*\\\\+,-\\\\.@_\\\\:;\\\\=\\\\\/0-9a-z]|(%[0-9a-f]{2}))*)?(?:\\\\?([\\\\!\"\\\\$&\'\\\\(\\\\)\\\\*\\\\+,-\\\\.@_\\\\:;\\\\=\\\\\/0-9a-z]|(%[0-9a-f]{2}))*)?(?:#([\\\\!\"\\\\$&\'\\\\(\\\\)\\\\*\\\\+,-\\\\.@_\\\\:;\\\\=\\\\\/0-9a-z]|(%[0-9a-f]{2}))*)?$\/i","message":"There was a problem with the field Url"}]}';
$this->assertEqual($expected, $validation);
}
}
View
38 vendors/js/unit_tests.js
@@ -119,5 +119,39 @@ $(function(){
ok( !$.fn.validate.validateRule("49.5", validationRules.JsValidateNumeric[0].rule), "49.5");
ok( !$.fn.validate.validateRule("forty nine", validationRules.JsValidateNumeric[0].rule), "forty nine");
});
-});
-
+
+ test("notEmpty", function() {
+ ok( $.fn.validate.validateRule("whatever", validationRules.JsValidateNotEmpty[0].rule), "whatever");
+ ok( !$.fn.validate.validateRule("", validationRules.JsValidateNotEmpty[0].rule), "<blank>");
+ });
+
+ test("phone", function() {
+ ok( $.fn.validate.validateRule("555-555-5555", validationRules.JsValidatePhone[0].rule), "555-555-5555");
+ ok( !$.fn.validate.validateRule("555-5a5-5555", validationRules.JsValidatePhone[0].rule), "555-5a5-5555");
+ });
+
+ test("postal", function() {
+ ok( $.fn.validate.validateRule("12345", validationRules.JsValidatePostal[0].rule), "12345");
+ ok( $.fn.validate.validateRule("12345-9876", validationRules.JsValidatePostal[0].rule), "12345-9876");
+ ok( !$.fn.validate.validateRule("1234a-9876", validationRules.JsValidatePostal[0].rule), "1234a-9876");
+ });
+
+ test("range", function() {
+ ok( $.fn.validate.validateRule("5", validationRules.JsValidateRange[0].rule), "5");
+ ok( $.fn.validate.validateRule("0", validationRules.JsValidateRange[0].rule), "0");
+ ok( $.fn.validate.validateRule("10", validationRules.JsValidateRange[0].rule), "10");
+ ok( !$.fn.validate.validateRule("-1", validationRules.JsValidateRange[0].rule), "-1");
+ ok( !$.fn.validate.validateRule("12", validationRules.JsValidateRange[0].rule), "12");
+ });
+
+ test("ssn", function() {
+ ok( $.fn.validate.validateRule("555-55-5555", validationRules.JsValidateSsn[0].rule), "555-55-5555");
+ ok( !$.fn.validate.validateRule("5a5-55-5555", validationRules.JsValidateSsn[0].rule), "55-5a5-5555");
+ });
+
+ test("url", function() {
+ ok( $.fn.validate.validateRule("www.pseudocoder.com", validationRules.JsValidateUrl[0].rule), "www.pseudocoder.com");
+ ok( $.fn.validate.validateRule("http://cakephp.org", validationRules.JsValidateUrl[0].rule), "http://cakephp.org");
+ ok( !$.fn.validate.validateRule("ww.zendcm", validationRules.JsValidateUrl[0].rule), "ww.zendcm");
+ });
+});
View
14 views/js_validate/test.ctp
@@ -1,12 +1,10 @@
+<?php $javascript->codeBlock('var validationRules = ' . $validation->bind('JsValidate', array('form' => false)) . ';',
+ array('inline' => false)); ?>
+
<?php echo $javascript->link(array('http://jqueryjs.googlecode.com/svn/trunk/qunit/testrunner.js',
- '/js_validate/js/jquery.validation')); ?>
-<?php echo $html->css(array('http://jqueryjs.googlecode.com/svn/trunk/qunit/testsuite.css')); ?>
-
-<script type="text/javascript">
- var validationRules = <?php echo $validation->bind('JsValidate', array('form' => false)) ?>;
-</script>
-
-<?php echo $javascript->link(array('/js_validate/js/unit_tests')); ?>
+ '/js_validate/js/unit_tests'),
+ false); ?>
+<?php echo $html->css(array('http://jqueryjs.googlecode.com/svn/trunk/qunit/testsuite.css'), null, null, false); ?>
<h1>CakePHP jQuery Validation Unit Tests</h1>
<h2 id="banner"></h2>
View
66 views/layouts/default.ctp
@@ -0,0 +1,66 @@
+<?php
+/* SVN FILE: $Id: default.ctp 7945 2008-12-19 02:16:01Z gwoo $ */
+/**
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
+ * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
+ * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
+ * @package cake
+ * @subpackage cake.cake.libs.view.templates.layouts
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @version $Revision: 7945 $
+ * @modifiedby $LastChangedBy: gwoo $
+ * @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+ <?php echo $html->charset(); ?>
+ <title>
+ <?php __('CakePHP: the rapid development php framework:'); ?>
+ <?php echo $title_for_layout; ?>
+ </title>
+ <?php
+ echo $html->meta('icon');
+
+ echo $javascript->link(array('http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
+ '/js_validate/js/jquery.validation'));
+ echo $html->css('cake.generic');
+
+ echo $scripts_for_layout;
+ ?>
+</head>
+<body>
+ <div id="container">
+ <div id="header">
+ <h1><?php echo $html->link(__('CakePHP: the rapid development php framework', true), 'http://cakephp.org'); ?></h1>
+ </div>
+ <div id="content">
+
+ <?php $session->flash(); ?>
+
+ <?php echo $content_for_layout; ?>
+
+ </div>
+ <div id="footer">
+ <?php echo $html->link(
+ $html->image('cake.power.gif', array('alt'=> __("CakePHP: the rapid development php framework", true), 'border'=>"0")),
+ 'http://www.cakephp.org/',
+ array('target'=>'_blank'), null, false
+ );
+ ?>
+ </div>
+ </div>
+ <?php echo $cakeDebug; ?>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.