OSDN Git Service

- fixed encoding dependent method in Ethna_Plugin_Validator_Min.php
authormumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Fri, 23 May 2008 20:02:17 +0000 (20:02 +0000)
committermumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Fri, 23 May 2008 20:02:17 +0000 (20:02 +0000)
CHANGES
class/Plugin/Validator/Ethna_Plugin_Validator_Min.php
test/Ethna_ActionForm_Test.php
test/Ethna_ActionForm_Validator_Min_Test.php [new file with mode: 0644]
test/Ethna_UnitTestBase.php

diff --git a/CHANGES b/CHANGES
index 19b048e..66f40d5 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,8 @@
 -- 但し、日本語のソースコードコメントはそのまま
 -- [Breaking B.C] フレームワークで扱う内部エンコーディング(mb_internal_encoding)もUTF-8に変更。但し、これは Ethna_Controller#_getDefaultLanguage
    をオーバーライドし、クライアントエンコーディングの値を変えることで変更可能です。
+-- 内部エンコーディングの変更に伴い、動作しなくなった箇所を修正
+--- Ethna_Plugin_Validator_Min.php
 - 国際化 (i18n) のための機能追加および変更
 -- [Breaking B.C] 言語名として解釈していた部分をロケール名に変更
 --- これにより、[appid]template/ja, [appid]/locale/ja の「ja」の部分が ja_JP に置き換わります。よって、古いバージョンから移行する場合はディレクトリ名の変更が必要です。
index 0e97348..fed53e1 100644 (file)
@@ -87,7 +87,7 @@ class Ethna_Plugin_Validator_Min extends Ethna_Plugin_Validator
                 break;
 
             case VAR_TYPE_STRING:
-                if (strlen($var) < $params['min']) {
+                if (mb_strlen($var) < $params['min']) {
                     if (isset($params['error'])) {
                         $msg = $params['error'];
                     } else {
index d73e834..9fe0971 100644 (file)
  *  @version    $Id$
  */
 
-// {{{ Test ActionForm Classes
-// {{{ Test_ActionForm_Integer
-/**
- *  integer type value Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_Integer extends Ethna_ActionForm
-{
-    var $form = array(
-        
-        //
-        //   integer type.
-        //
-
-        //   integer type error
-        'integer_type_error_input' => array(
-            'required' => false,
-            'type' => VAR_TYPE_INT,
-         ),
-        //   required input
-        'integer_required_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_INT,
-         ),
-    
-        //   minimun input
-        'integer_min_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_INT,
-            'min' => 100,
-         ),
-
-         //  maximum input
-         'integer_max_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_INT,
-            'max' => 100,
-         ),
-         //  regexp input
-         'integer_regexp_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_INT,
-            'regexp' => '/^\d+$/',
-         ),
-
-    ); 
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_Integer
-/**
- *  integer type value Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_Integer extends Test_ActionForm_Integer
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_ActionForm_Float
-/**
- *  float value type Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_Float extends Ethna_ActionForm
-{
-    var $form = array(
-        
-        //
-        //   float type.
-        //
-
-        //   float type error
-        'float_type_error_input' => array(
-            'required' => false,
-            'type' => VAR_TYPE_FLOAT,
-         ),
-        //   required input
-        'float_required_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_FLOAT,
-         ),
-    
-        //   minimun input
-        'float_min_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_FLOAT,
-            'min' => 100.1,
-         ),
-
-         //  maximum input
-         'float_max_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_FLOAT,
-            'max' => 100.1,
-         ),
-         //  regexp input
-         'float_regexp_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_FLOAT,
-            'regexp' => '/^\d+\.\d+$/',
-         ),
-
-    ); 
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_Float
-/**
- *  float type filling default value Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_Float extends Test_ActionForm_Float
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_ActionForm_String
-/**
- *  string value type Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_String extends Ethna_ActionForm
-{
-    var $form = array(
-        
-        //
-        //   string type.
-        //
-
-        //   required input
-        'string_required_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_STRING,
-         ),
-    
-        //   minimun input
-        'string_min_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_STRING,
-            'min' => 5,
-         ),
-
-         //  maximum input
-         'string_max_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_STRING,
-            'max' => 5,
-         ),
-         //  regexp input
-         'string_regexp_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_STRING,
-            'regexp' => '/^b+$/',
-         ),
-    ); 
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_String
-/**
- *  string type value Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_String extends Test_ActionForm_String
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_ActionForm_Datetime
-/**
- *  datetime value type Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_Datetime extends Ethna_ActionForm
-{
-    var $form = array(
-        
-        //
-        //   datetime type.
-        //
-
-        //   required input
-        'datetime_required_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_DATETIME,
-         ),
-    
-        //   minimun input
-        'datetime_min_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_DATETIME,
-            'min' => '2010-10-01',
-         ),
-
-         //  maximum input
-         'datetime_max_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_DATETIME,
-            'max' => '2000-01-05',
-         ),
-         //  regexp input
-         'datetime_regexp_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_DATETIME,
-            'regexp' => '/^\d{4}-\d{2}-\d{2}$/',
-         ),
-    ); 
-}
-// }}}
-
-// {{{ Test_ActionForm_Boolean
-/**
- *  boolean value type Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_Boolean extends Ethna_ActionForm
-{
-    var $form = array(
-        
-        //
-        //   boolean type.
-        //
-
-        //   required input
-        'boolean_required_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_BOOLEAN,
-         ),
-    ); 
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_Boolean
-/**
- *  boolean type filling default value Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_Boolean extends Test_ActionForm_Boolean
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_Datetime
-/**
- *  datetime type Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_Datetime extends Test_ActionForm_Datetime
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_ActionForm_File
-/**
- *  file value type Test ActionForm
- *
- *  @access private
- */
-class Test_ActionForm_File extends Ethna_ActionForm
-{
-    var $form = array(
-         //
-         //  file input
-         //
-         'file_input' => array(
-            'required' => true,
-            'type' => VAR_TYPE_FILE,
-         ),
-    );
-
-}
-// }}}
-
-// {{{ Test_ActionForm_WithPlugin_File
-/**
- *  Test ActionForm
- *  with use_validator_plugin flag is on.
- *
- *  @access private
- */
-class Test_ActionForm_WithPlugin_File extends Test_ActionForm_File
-{
-    var $use_validator_plugin = true;
-}
-// }}}
-// }}}
-
 // {{{    Ethna_ActionForm_Test
 /**
- *  Test Case For Ethna_ActionForm
+ *  Test Case For Ethna_ActionForm(Mainly Validator)
  *
  *  @access public
  */
 class Ethna_ActionForm_Test extends Ethna_UnitTestBase
 {
-    var $ctl;
-
-    // {{{ setUp, tearDown
     function setUp()
     {
-        //    dummy AUTO GLOBAL VALUE
-        $_SERVER['REQUEST_METHOD'] = 'POST';
-
-        //    initialize controller and clear error.
-        $this->ctl =& Ethna_Controller::getInstance();
-        $ae =& $this->ctl->getActionError();
-        $ae->clear();
-    }
-
-    function tearDown()
-    {
-        unset($_SERVER['REQUEST_METHOD']);
-        unset($this->ctl);
-    }
-    // }}}
-
-    // {{{    test_Validate_Integer
-    function test_Validate_Integer()
-    {
-        //    normal test value
-        $normal_input = array(
-                        'integer_required_input' => 100,
-                        'integer_min_input' => 1000,
-                        'integer_max_input' => 10,
-                        'integer_regexp_input' => 50000,
-                      );
-
-        $this->_run('integer', $normal_input);
-
-        //    error test value
-        $error_input = array(
-                        'integer_type_error_input' => 'aaaa',
-                        'integer_required_input' => null,
-                        'integer_min_input' => -10000,
-                        'integer_max_input' => 10000,
-                        'integer_regexp_input' => 'aaa',
-                    );
-
-        $this->_run('integer', $error_input, true);
-    }
-    // }}}
-
-    // {{{    test_Validate_Float
-    function test_Validate_Float()
-    {
-        //    normal test value
-        $normal_input = array(
-                        'float_required_input' => 10.1,
-                        'float_min_input' => 10000.1,
-                        'float_max_input' => 10.1,
-                        'float_regexp_input' => 50.1,
-                      );
-
-        $this->_run('float', $normal_input);
-
-        //    error test value
-        $error_input = array(
-                        'float_type_error_input' => 'aaaa',
-                        'float_required_input' => null,
-                        'float_min_input' => -10000,
-                        'float_max_input' => 10000,
-                        'float_regexp_input' => 'aaa',
-                    );
-        $this->_run('float', $error_input, true);
-    }
-    // }}}
-
-    // {{{    test_Validate_String
-    function test_Validate_String()
-    {
-        //    normal test value
-        $normal_input = array(
-                        'string_required_input' => 'aaaa',
-                        'string_min_input' => 'aaaaaaa',
-                        'string_max_input' => 'bbb',
-                        'string_regexp_input' => 'bbb',
-                    );
-        $this->_run('string', $normal_input);
-
-        //    error test value
-        $error_input = array(
-                        'string_required_input' => null,
-                        'string_min_input' => 'a',
-                        'string_max_input' => 'bbbbbbbbbbbbbb',
-                        'string_regexp_input' => 'aaa',
-                    );
-        $this->_run('string', $error_input, true);
-    }
-    // }}}
-
-    // {{{    test_Validate_DateTime
-    function test_Validate_Datetime()
-    {
-        $normal_input = array(
-                        'datetime_required_input' => '2007-04-01',
-                        'datetime_min_input' => '2030-01-01',
-                        'datetime_max_input' => '1999-01-01',
-                        'datetime_regexp_input' => '2007-11-30',
-                    );
-
-        $this->_run('datetime', $normal_input);
-
-        //    error test value
-        $error_input = array(
-                        'datetime_required_input' => null,
-                        'datetime_min_input' => '2007-01-01',
-                        'datetime_max_input' => '2007-01-01',
-                        'datetime_regexp_input' => '2007a-11-ga',
-                    );
-
-        $this->_run('datetime', $error_input, true);
-    }
-    // }}}
-
-    // {{{    test_Validate_Boolean
-    function test_Validate_Boolean()
-    {
-        $normal_input = array(
-                        'boolean_required_input' => true,
-                    );
-
-        $this->_run('boolean', $normal_input);
-
-        //    error test value
-        $error_input = array(
-                        'boolean_required_input' => null,
-                    );
-
-        $this->_run('boolean', $error_input, true);
-    }
-    // }}}
-
-    // {{{ private utility functions
-    function _run($type_name, $input, $test_error = false)
-    {
-        //    test with no plugin 
-        $this->_setInputValue($input);
-        $af = $this->_get_validated_af_normal($type_name);
-        $ae = $this->_get_action_error();
-
-        //    finally assert!
-        $this->_assert($input, $ae, $test_error);
-
-        //    test with plugin 
-        $af = $this->_get_validated_af_normal($type_name, true);
-        $ae = $this->_get_action_error();
-
-        //    finally assert!(with plugin)
-        $this->_assert($input, $ae, $test_error);
-
-        //    unset input value
-        $this-> _unsetInputValue($input);
-    }
-
-    function _setInputValue($input)
-    {
-        foreach ($input as $key => $value) {
-            $_POST[$key] = $value;
-        }
-    }
-
-    function _unsetInputValue($input)
-    {
-        foreach ($input as $key => $value) {
-            unset($_POST[$key]);
-        }
-    }
-
-    function _get_validated_af_normal($type_name, $is_plugin = false)
-    {
-         $class_name_prefix = ($is_plugin)
-                           ? 'Test_ActionForm_WithPlugin_'
-                           : 'Test_ActionForm_';
-         $class_name = $class_name_prefix . ucfirst($type_name);
-         return $this->_get_validated_af_real($class_name, $is_plugin);
-    }
-
-    function _get_validated_af_default($type_name, $is_plugin = false)
-    {
-        $class_name_prefix = ($is_plugin)
-                           ? 'Test_Default_ActionForm_WithPlugin_'
-                           : 'Test_Default_ActionForm_';
-        $class_name = $class_name_prefix . ucfirst($type_name);
-
-        return $this->_get_validated_af_real($class_name, $is_plugin);
-   }
-
-    function _get_validated_af_real($class_name, $is_plugin = false)
-    {
-        $dummy_af = new $class_name($this->ctl);
-        $dummy_af->setFormVars();
-
-        //
-        //    run validate
-        //    we ignore return value, because it includes
-        //    input value error in other test case ...
-        //
-        $dummy_af->validate();
-        return $dummy_af;
-    }
-    
-    function _get_action_error()
-    {
-        $ae  =& $this->ctl->getActionError();
-        return $ae;
-    }
-
-    function _assert($input, $ae, $test_error = false)
-    {
-        foreach($input as $key => $value)
-        {
-            if ($test_error) {
-                $this->assertTrue($ae->isError($key));
-            } else {
-                $this->assertFalse($ae->isError($key));
-            }
-        } 
-    }
-
-    function _assert_default($expected, $af, $ae)
-    {
-        foreach($expected as $key => $value)
-        {
-            //    error must not exists
-            //    in case of filling default value.
-            $this->assertEqual($af->get($key), $value);
-
-            //    we must get default value.
-            $this->assertFalse($ae->isError($key));
-        } 
+        $this->af->use_validator_plugin = false;
+        $this->af->clearFormVars();
+        $this->af->form = array();
+        $this->ae->clear();
     }
-    // }}}
 }
 // }}}
 
diff --git a/test/Ethna_ActionForm_Validator_Min_Test.php b/test/Ethna_ActionForm_Validator_Min_Test.php
new file mode 100644 (file)
index 0000000..711e7b3
--- /dev/null
@@ -0,0 +1,158 @@
+<?php
+// vim: foldmethod=marker
+/**
+ *  Ethna_ActionForm_Validator_Min_Test.php
+ *
+ *  @author     Yoshinari Takaoka <takaoka@beatcraft.com>
+ *  @version    $Id: Ethna_ActionForm_Test.php 494 2008-04-05 19:04:17Z mumumu-org $
+ */
+
+// {{{    Ethna_ActionForm_Validator_Min_Test
+/**
+ *  Test Case For Ethna_ActionForm(Min Validator)
+ *
+ *  @access public
+ */
+class Ethna_ActionForm_Validator_Min_Test extends Ethna_UnitTestBase
+{
+    function setUp()
+    {
+        $this->af->use_validator_plugin = false;
+        $this->af->clearFormVars();
+        $this->af->form = array();
+        $this->ae->clear();
+    }
+
+    // {{{ Validator Min Integer. 
+    function test_Validate_Min_Integer()
+    {
+        $form_def = array(
+                        'type' => VAR_TYPE_INT,
+                        'form_type' => FORM_TYPE_TEXT,
+                        'required' => true,
+                        'min' => 5,
+                    );        
+        $this->af->setDef('input', $form_def);
+        
+        $this->af->set('input', 5);
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 4); 
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+        $this->ae->clear();
+        $this->af->set('input', 6);
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+    }
+    // }}}
+
+    // {{{ Validator Min Float. 
+    function test_Validate_Min_Float()
+    {
+        $form_def = array(
+                        'type' => VAR_TYPE_FLOAT,
+                        'form_type' => FORM_TYPE_TEXT,
+                        'required' => true,
+                        'min' => 5,
+                    );        
+        $this->af->setDef('input', $form_def);
+        
+        $this->af->set('input', 4.999999); 
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 5.0);
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 5);
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 4);
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+    }
+    // }}}
+
+    // {{{ Validator Min Datetime. 
+    function test_Validate_Min_DateTime()
+    {
+        $form_def = array(
+                        'type' => VAR_TYPE_DATETIME,
+                        'form_type' => FORM_TYPE_TEXT,
+                        'required' => true,
+                        'min' => '2000-01-01',
+                    );        
+        $this->af->setDef('input', $form_def);
+        
+        $this->af->set('input', '1999-12-31'); 
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', '2000-01-01');
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', '2000-01-02');
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+    }
+    // }}}
+
+    // {{{ Validator Min String. 
+    function test_Validate_Min_String()
+    {
+        $form_def = array(
+                        'type' => VAR_TYPE_STRING,
+                        'form_type' => FORM_TYPE_TEXT,
+                        'required' => true,
+                        'min' => 5,
+                    );        
+        $this->af->setDef('input', $form_def);
+        
+        //   in ascii.
+        $this->af->set('input', 'abcd'); 
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 'abcde');
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        //   multibyte.
+        $this->af->set('input', 'あいうえお');
+        $this->af->validate();
+        $this->assertFalse($this->ae->isError('input'));
+        $this->ae->clear();
+
+        $this->af->set('input', 'あいうえ');
+        $this->af->validate();
+        $this->assertTrue($this->ae->isError('input'));
+    }
+    // }}}
+
+    // {{{ Validator Min File. 
+    function test_Validate_Min_File()
+    {
+        //  skipped because we can't bypass 
+        //  is_uploaded_file function.
+    }
+    // }}}
+
+}
+// }}}
+
+?>
index 1981842..3ab7240 100644 (file)
@@ -20,6 +20,12 @@ class Ethna_UnitTestBase extends UnitTestCase
     /** @var    object  Ethna_Controller    コントローラオブジェクト($controllerの省略形) */
     var $ctl;
 
+    /** @var    object  Ethna_ActionForm    アクションフォームオブジェクト($action_formの省略形) */
+    var $af;
+
+    /** @var    object  Ethna_ActionError    アクションエラーオブジェクト($action_errorの省略形) */
+    var $ae;
+
     function Ethna_UnitTestBase($label = false)
     {
         parent::UnitTestCase($label);
@@ -34,11 +40,13 @@ class Ethna_UnitTestBase extends UnitTestCase
         // backend
         $this->backend =& $this->ctl->getBackend();
 
-        // actionform
+        // actionform, actionerror.
         if ($this->ctl->action_form === null) {
             $this->ctl->action_form =& new Ethna_ActionForm($this->ctl);
             $this->backend->setActionForm($this->ctl->action_form);
         }
+        $this->af =& $this->ctl->action_form;
+        $this->ae =& $this->ctl->getActionError();
 
         // viewclass
         if ($this->ctl->view === null) {
@@ -46,4 +54,5 @@ class Ethna_UnitTestBase extends UnitTestCase
         }
     }
 }
+
 ?>