OSDN Git Service

9385b1b46b4f5177100b537d2191d8a027b1a522
[ethna/ethna.git] / test / Ethna_ActionForm_Test.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_ActionForm_Test.php
5  *
6  *  @author     Yoshinari Takaoka <takaoka@beatcraft.com>
7  *  @version    $Id$
8  */
9
10 // {{{    Ethna_Test_ActionForm
11 /**
12  *  Test ActionForm 
13  *
14  *  @access public
15  */
16 class Ethna_Test_ActionForm extends Ethna_ActionForm
17 {
18     var $form = array(
19         'test' => array(
20             'type' => VAR_TYPE_STRING,
21             'form_type' => FORM_TYPE_TEXT,
22             'name' => 'test',
23         ),
24
25         'no_name' => array(
26             'type' => VAR_TYPE_STRING,
27             'form_type' => FORM_TYPE_TEXT,
28         ),
29
30         'test_array' => array(
31             'type' => array(VAR_TYPE_STRING),
32             'form_type' => FORM_TYPE_TEXT,
33             'name' => 'test array',
34         ),
35
36     );
37 }
38 // }}}
39
40 // {{{    Ethna_ActionForm_Test
41 /**
42  *  Test Case For Ethna_ActionForm(Mainly Validator)
43  *
44  *  @access public
45  */
46 class Ethna_ActionForm_Test extends Ethna_UnitTestBase
47 {
48     var $local_af;
49
50     function setUp()
51     {
52         //   REQUEST_METHOD を設定しないと
53         //   フォームテンプレートが初期化されない
54         $_SERVER['REQUEST_METHOD'] = 'POST';
55         $this->local_af = new Ethna_Test_ActionForm($this->ctl); 
56         $this->local_af->clearFormVars();
57         $this->ae->clear();
58     }
59
60     function tearDown()
61     {
62         $_SERVER['REQUEST_METHOD'] = NULL;
63         $this->local_af = NULL;
64         $_POST = array();
65     }
66
67     // {{{ get
68     function test_get()
69     {
70         $this->local_af->set('test', 'test');
71         $this->assertEqual('test', $this->local_af->get('test'));
72     }
73     // }}}
74
75     // {{{ getDef
76     function test_getDef()
77     {
78         //   null param.
79         $def = $this->local_af->getDef();
80         $this->assertEqual(3, count($def));
81         $this->assertEqual(10, count($def['test']));
82         $this->assertEqual('test', $def['test']['name']);
83
84         //   non-exist key.
85         $this->assertNull($this->local_af->getDef('hoge'));
86
87         $def = $this->local_af->getDef('test');
88         $this->assertEqual(10, count($def));
89         $this->assertEqual('test', $def['name']);
90     }
91     // }}}
92
93     // {{{ getName
94     function test_getName()
95     {
96         $this->assertNull($this->local_af->getName('hoge'));
97         $this->assertEqual('test', $this->local_af->getName('test'));
98         
99         //   もしフォームのname属性がないと、keyそのものが返ってくる
100         $this->assertEqual('no_name', $this->local_af->getName('no_name'));
101     }
102     // }}}
103
104     // {{{ clearFormVars
105     function test_clearFormVars()
106     {
107         $this->local_af->set('test', 'hoge');
108         $this->local_af->set('no_name', 'fuga');
109
110         $this->local_af->clearFormVars();
111
112         $this->assertNull($this->local_af->get('test'));
113         $this->assertNull($this->local_af->get('no_name'));
114     }
115     // }}}
116
117     // {{{ set 
118     function test_set()
119     {
120         $this->local_af->set('test', 'test');
121         $this->assertEqual('test', $this->local_af->get('test'));
122     }
123     // }}}
124
125     // {{{ getHiddenVars 
126     function test_getHiddenVars()
127     {
128         //    フォーム定義が配列で、submit された値が非配列の場合
129         //    かつ、フォーム定義が配列なので、結局出力するhiddden
130         //    タグも配列用のものとなる. 警告も勿論でない
131         $this->local_af->set('test_array', 1);
132
133         $hidden = $this->local_af->getHiddenVars();
134         $expected = "<input type=\"hidden\" name=\"test_array[0]\" value=\"1\" />\n";
135         $this->assertEqual($hidden, $expected);
136         $this->local_af->clearFormVars();
137
138         //    配列出力のテスト
139         $this->local_af->set('test_array', array(1, 2));
140         $hidden = $this->local_af->getHiddenVars();
141         $expected = "<input type=\"hidden\" name=\"test_array[0]\" value=\"1\" />\n"
142                   . "<input type=\"hidden\" name=\"test_array[1]\" value=\"2\" />\n";
143         $this->assertEqual($hidden, $expected);
144         $this->local_af->clearFormVars();
145
146         //    スカラーのテスト
147         $this->local_af->set('test', 1);
148         $hidden = $this->local_af->getHiddenVars();
149         $expected = "<input type=\"hidden\" name=\"test\" value=\"1\" />\n";
150         $this->assertEqual($hidden, $expected);
151         $this->local_af->clearFormVars();
152
153         //    フォーム定義がスカラーで、submitされた値が配列の場合
154         //    この場合は明らかに使い方が間違っている上、2重に値が
155         //    出力されても意味がないので、警告(E_NOTICE)扱いにする
156         //    この場合、hiddenタグは出力されない
157         $this->local_af->set('test', array(1,2));
158         $hidden = $this->local_af->getHiddenVars();
159         $this->assertEqual($hidden, '');  //  値が入っていない扱いなので空文字が返る
160         $this->local_af->clearFormVars();
161
162         //    include_list テスト
163         $this->local_af->set('test', 1);
164         $this->local_af->set('no_name', 'name');
165         $this->local_af->set('test_array', array(1,2));
166         $include_list = array('test');
167         $hidden = $this->local_af->getHiddenVars($include_list);
168         $expected = "<input type=\"hidden\" name=\"test\" value=\"1\" />\n";
169         $this->assertEqual($hidden, $expected);
170        
171         //    exclude_list テスト
172         $exclude_list = array('test_array', 'no_name');
173         $hidden = $this->local_af->getHiddenVars(NULL, $exclude_list);
174         $expected = "<input type=\"hidden\" name=\"test\" value=\"1\" />\n";
175         $this->assertEqual($hidden, $expected);
176
177         //    include_list, exclude_list の組み合わせ
178         $include_list = array('test', 'no_name');
179         $exclude_list = array('no_name');
180         $hidden = $this->local_af->getHiddenVars($include_list, $exclude_list);
181         $expected = "<input type=\"hidden\" name=\"test\" value=\"1\" />\n";
182         $this->assertEqual($hidden, $expected);
183     }
184     // }}}
185
186 }
187 // }}}
188