1+ <?php
2+ /**
3+ * @package testing
4+ * @copyright (c) 2014 phpBB Group
5+ * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
6+ *
7+ */
8+
9+ class phpbb_profilefield_type_dropdown_test extends phpbb_test_case
10+ {
11+ protected $ cp ;
12+ protected $ field_options = array ();
13+ protected $ dropdown_options = array ();
14+
15+ /**
16+ * Sets up basic test objects
17+ *
18+ * @access public
19+ * @return void
20+ */
21+ public function setUp ()
22+ {
23+ global $ request , $ user , $ cache , $ db , $ table_prefix ;
24+
25+ require_once dirname (__FILE__ ) . '/../../phpBB/includes/functions.php ' ;
26+ require_once dirname (__FILE__ ) . '/../../phpBB/includes/functions_content.php ' ;
27+ require_once dirname (__FILE__ ) . '/../../phpBB/includes/utf/utf_tools.php ' ;
28+
29+ $ user = $ this ->getMock ('\phpbb\user ' );
30+ $ cache = new phpbb_mock_cache ;
31+ $ user ->expects ($ this ->any ())
32+ ->method ('lang ' )
33+ ->will ($ this ->returnCallback (array ($ this , 'return_callback_implode ' )));
34+
35+ $ request = $ this ->getMock ('\phpbb\request\request ' );
36+ $ template = $ this ->getMock ('\phpbb\template\template ' );
37+
38+ $ lang = $ this ->getMock ('\phpbb\profilefields\lang_helper ' , array (), array ($ db , $ table_prefix . 'profile_fields_lang ' ));
39+
40+ $ lang ->expects ($ this ->any ())
41+ ->method ('get_options_lang ' );
42+
43+ $ lang ->expects ($ this ->any ())
44+ ->method ('is_set ' )
45+ ->will ($ this ->returnCallback (array ($ this , 'is_set_callback ' )));
46+
47+ $ lang ->expects ($ this ->any ())
48+ ->method ('get ' )
49+ ->will ($ this ->returnCallback (array ($ this , 'get ' )));
50+
51+ $ this ->cp = new \phpbb \profilefields \type \type_dropdown (
52+ $ lang ,
53+ $ request ,
54+ $ template ,
55+ $ user
56+ );
57+
58+ $ this ->field_options = array (
59+ 'field_type ' => '\phpbb\profilefields\type\type_dropdown ' ,
60+ 'field_name ' => 'field ' ,
61+ 'field_id ' => 1 ,
62+ 'lang_id ' => 1 ,
63+ 'lang_name ' => 'field ' ,
64+ 'field_required ' => false ,
65+ 'field_validation ' => '.* ' ,
66+ 'field_novalue ' => 0 ,
67+ );
68+
69+ $ this ->dropdown_options = array (
70+ 0 => '<No Value> ' ,
71+ 1 => 'Option 1 ' ,
72+ 2 => 'Option 2 ' ,
73+ 3 => 'Option 3 ' ,
74+ 4 => 'Option 4 ' ,
75+ );
76+ }
77+
78+ public function get_validate_profile_field_data ()
79+ {
80+ return array (
81+ array (
82+ 7 ,
83+ array (),
84+ 'FIELD_INVALID_VALUE-field ' ,
85+ 'Invalid value should throw error ' ,
86+ ),
87+ array (
88+ 2 ,
89+ array (),
90+ false ,
91+ 'Valid value should not throw error '
92+ ),
93+ array (
94+ 0 ,
95+ array (),
96+ false ,
97+ 'Empty value should be acceptible ' ,
98+ ),
99+ array (
100+ 0 ,
101+ array ('field_required ' => true ),
102+ 'FIELD_REQUIRED-field ' ,
103+ 'Required field should not accept empty value ' ,
104+ ),
105+ );
106+ }
107+
108+ /**
109+ * @dataProvider get_validate_profile_field_data
110+ */
111+ public function test_validate_profile_field ($ value , $ field_options , $ expected , $ description )
112+ {
113+ $ field_options = array_merge ($ this ->field_options , $ field_options );
114+
115+ $ result = $ this ->cp ->validate_profile_field ($ value , $ field_options );
116+
117+ $ this ->assertSame ($ expected , $ result , $ description );
118+ }
119+
120+ public function get_profile_value_data ()
121+ {
122+ return array (
123+ array (
124+ 1 ,
125+ array ('field_show_novalue ' => true ),
126+ 'Option 1 ' ,
127+ 'Field should output the given value ' ,
128+ ),
129+ array (
130+ 4 ,
131+ array ('field_show_novalue ' => false ),
132+ 'Option 4 ' ,
133+ 'Field should output the given value ' ,
134+ ),
135+ array (
136+ '' ,
137+ array ('field_show_novalue ' => true ),
138+ '<No Value> ' ,
139+ 'Field should output nothing for empty value ' ,
140+ ),
141+ array (
142+ '' ,
143+ array ('field_show_novalue ' => false ),
144+ null ,
145+ 'Field should simply output null for empty value ' ,
146+ ),
147+ );
148+ }
149+
150+
151+ /**
152+ * @dataProvider get_profile_value_data
153+ */
154+ public function test_get_profile_value ($ value , $ field_options , $ expected , $ description )
155+ {
156+ $ field_options = array_merge ($ this ->field_options , $ field_options );
157+
158+ $ result = $ this ->cp ->get_profile_value ($ value , $ field_options );
159+
160+ $ this ->assertSame ($ expected , $ result , $ description );
161+ }
162+
163+ public function is_set_callback ($ field_id , $ lang_id , $ field_value )
164+ {
165+ return isset ($ this ->dropdown_options [$ field_value ]);
166+ }
167+
168+ public function get ($ field_id , $ lang_id , $ field_value )
169+ {
170+ return $ this ->dropdown_options [$ field_value ];
171+ }
172+
173+ public function return_callback_implode ()
174+ {
175+ return implode ('- ' , func_get_args ());
176+ }
177+ }
0 commit comments