~ubuntu-branches/ubuntu/vivid/zentyal-core/vivid-proposed

« back to all changes in this revision

Viewing changes to src/EBox/Types/t/Select.t

  • Committer: Package Import Robot
  • Author(s): Jorge Salamero Sanz
  • Date: 2012-08-28 10:03:33 UTC
  • Revision ID: package-import@ubuntu.com-20120828100333-oz3n5kpav4z0tl27
Tags: 2.3.21+quantal1
New upstream release for Quantal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2012 eBox Technologies S.L.
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License, version 2, as
5
 
# published by the Free Software Foundation.
6
 
#
7
 
# This program is distributed in the hope that it will be useful,
8
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
# GNU General Public License for more details.
11
 
#
12
 
# You should have received a copy of the GNU General Public License
13
 
# along with this program; if not, write to the Free Software
14
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 
 
16
 
 
17
 
use strict;
18
 
use warnings;
19
 
 
20
 
use Test::More tests => 20;
21
 
use Test::MockObject;
22
 
use Test::Exception;
23
 
 
24
 
use Error qw(:try);
25
 
 
26
 
use EBox::TestStubs;
27
 
 
28
 
 
29
 
use lib '../../..';
30
 
 
31
 
use EBox::Types::Test;
32
 
use EBox::Types::Select;
33
 
use EBox::Types::Text;
34
 
use EBox::Model::DataTable;
35
 
 
36
 
 
37
 
 
38
 
 
39
 
 
40
 
sub creationTest
41
 
{
42
 
    my $class = 'EBox::Types::Select';
43
 
 
44
 
    EBox::Types::Test::createOk(
45
 
                                $class,
46
 
                                noSetCheck => 1,
47
 
 
48
 
                                fieldName => 'select',
49
 
                                editable  => 1,
50
 
                                populate => sub { return []  },
51
 
 
52
 
                                'select with populate option creation ok'
53
 
                               );
54
 
 
55
 
    EBox::Types::Test::createOk(
56
 
                                $class,
57
 
                                noSetCheck => 1,
58
 
 
59
 
                                fieldName => 'select',
60
 
                                editable  => 1,
61
 
                                foreignModel => \&_foreignModel,
62
 
                                foreignField => 'name',
63
 
 
64
 
                                'select with foreignModel and foreignField options creation ok'
65
 
                               );
66
 
 
67
 
    EBox::Types::Test::createFail(
68
 
                                  $class,
69
 
                                  noSetCheck => 1,
70
 
 
71
 
                                  fieldName => 'select',
72
 
                                  populate => sub { return []  },
73
 
 
74
 
                                  'select without editable option fails construction'
75
 
                                 );
76
 
 
77
 
 
78
 
    EBox::Types::Test::createFail(
79
 
                                  $class,
80
 
                                  noSetCheck => 1,
81
 
 
82
 
                                  fieldName => 'select',
83
 
                                  populate => sub { return []  },
84
 
                                  editable => 1,
85
 
                                  optional => 1,
86
 
 
87
 
                                  'select with optional option fails construction'
88
 
                                 );
89
 
 
90
 
 
91
 
    EBox::Types::Test::createFail(
92
 
                                  $class,
93
 
                                  noSetCheck => 1,
94
 
 
95
 
                                  fieldName => 'select',
96
 
                                  editable => 1,
97
 
                                  optional => 1,
98
 
 
99
 
                                  'select without either populate or foreignModel fails construction'
100
 
                                 );
101
 
 
102
 
    EBox::Types::Test::createFail(
103
 
                                  $class,
104
 
                                  noSetCheck => 1,
105
 
 
106
 
                                  fieldName => 'select',
107
 
                                  editable => 1,
108
 
                                  optional => 1,
109
 
                                  foreignModel => \&_foreignModel,
110
 
 
111
 
                                  'select with foreignModel but lacking foreignField fails construction'
112
 
                                 );
113
 
 
114
 
 
115
 
    EBox::Types::Test::createFail(
116
 
                                  $class,
117
 
                                  noSetCheck => 1,
118
 
 
119
 
                                  fieldName => 'select',
120
 
                                  editable => 1,
121
 
                                  optional => 1,
122
 
                                  foreignField => 'name', 
123
 
 
124
 
                                  'select with foreignField but lacking foreignModel fails construction'
125
 
                                 );
126
 
}
127
 
 
128
 
 
129
 
 
130
 
my $_foreignModel;
131
 
sub _foreignModel
132
 
{
133
 
    my ($self) = @_;
134
 
 
135
 
    if (not defined $_foreignModel) {
136
 
        $_foreignModel =  __PACKAGE__->_newPopulatedDataTable();
137
 
    }
138
 
 
139
 
    return $_foreignModel;
140
 
}
141
 
 
142
 
 
143
 
sub _newDataTable
144
 
{
145
 
    my ($self, $table) = @_;
146
 
    if (not defined $table) {
147
 
        die 'no table';
148
 
    }
149
 
 
150
 
    EBox::TestStubs::fakeEBoxModule(name => 'fakeModule');
151
 
    my $gconfmodule = EBox::Global->modInstance('fakeModule');
152
 
 
153
 
    my $dataTableDir = '/ebox/modules/fakeModule/DataTable';
154
 
    # remove old data from prvious modules
155
 
    $gconfmodule->delete_dir($dataTableDir);
156
 
 
157
 
 
158
 
    my $dataTableBase = EBox::Model::DataTable->new(
159
 
                                                 gconfmodule => $gconfmodule,
160
 
                                                 directory   => $dataTableDir,
161
 
                                                 domain      => 'domain',
162
 
                                                );
163
 
 
164
 
 
165
 
    my $dataTable = Test::MockObject::Extends->new($dataTableBase);
166
 
    $dataTable->set_always('_table' => $table);
167
 
 
168
 
 
169
 
    return $dataTable;
170
 
}
171
 
 
172
 
 
173
 
sub _newPopulatedDataTable
174
 
{
175
 
    my ($self) = @_;
176
 
 
177
 
    my $tableDescription = _tableDescription4fields();
178
 
 
179
 
 
180
 
    my $dataTable = $self->_newDataTable($tableDescription);
181
 
 
182
 
    my @values = (
183
 
 
184
 
                       [ name => 'a', regularField => 'regular' ],
185
 
                       [ 
186
 
                        name => 'b', regularField => 'regular', 
187
 
                        defaultField => 'noDefaultText' 
188
 
                       ],
189
 
                       [ 
190
 
                        name => 'c', regularField => 'regular', 
191
 
                        optionalField => 'noDefaultText' 
192
 
                       ],
193
 
                      );
194
 
 
195
 
    foreach (@values) {
196
 
        $dataTable->add( @{ $_  } );
197
 
    }
198
 
 
199
 
 
200
 
    return $dataTable;
201
 
}
202
 
 
203
 
 
204
 
 
205
 
 
206
 
sub _tableDescription4fields
207
 
{
208
 
      my $tableDescription = {
209
 
                  tableDescription => [
210
 
                                       new EBox::Types::Text(
211
 
                                                   fieldName => 'name',
212
 
                                                   printableName => 'name',
213
 
                                                   unique        => 1,
214
 
                                                                ),
215
 
                                       new EBox::Types::Text(
216
 
                                                  fieldName => 'regularField',
217
 
                                                 printableName => 'regularField',
218
 
                                                                ),
219
 
                                       new EBox::Types::Text(
220
 
                                                fieldName => 'defaultField',
221
 
                                                printableName => 'defaultField',
222
 
                                                defaultValue    => 'defaultText',
223
 
                                                                ),
224
 
                                       
225
 
                                       new EBox::Types::Text(
226
 
                                                fieldName => 'optionalField',
227
 
                                                printableName => 'optionalField',
228
 
                                                optional      => 1,
229
 
                                                                ),
230
 
                                                                   
231
 
                                      ],
232
 
                            tableName => 'test',
233
 
 
234
 
                           };
235
 
 
236
 
      return $tableDescription;
237
 
}
238
 
 
239
 
 
240
 
sub optionsFromPopulateTest
241
 
{
242
 
    my $populateOptions = [
243
 
                           { value => 'a' , printableValue => 'a' },
244
 
                           { value => 'ea' , printableValue => 'ea' },
245
 
                           { value => 'cas' , printableValue => 'cas' },
246
 
                          ];
247
 
 
248
 
    my $select =  new EBox::Types::Select(
249
 
                                          fieldName => 'select',
250
 
                                          editable  => 1,
251
 
                                          populate => sub { 
252
 
                                              return $populateOptions  
253
 
                                          },
254
 
                                         );
255
 
 
256
 
    is_deeply $select->options(), $populateOptions,
257
 
        'checking options returned from a select with populate attribute';
258
 
}
259
 
 
260
 
sub optionsFromForeignModelTest
261
 
{
262
 
    my $select = new EBox::Types::Select(
263
 
                                         fieldName => 'select',
264
 
                                         editable  => 1,
265
 
                                         foreignModel => \&_foreignModel,
266
 
                                         foreignField => 'name',
267
 
                                        );
268
 
 
269
 
    my @expected = map {
270
 
                          my $id = $_->id();
271
 
                          my $name = $_->valueByName('name');
272
 
                    {value => $id, printableValue => $name },
273
 
                   } @{ _foreignModel->rows() };
274
 
 
275
 
    my @actual = @{ $select->options() };
276
 
 
277
 
 
278
 
    is_deeply \@actual, \@expected,
279
 
        'checking options returned from a select with foreign model';
280
 
}
281
 
 
282
 
 
283
 
sub setValueTest
284
 
{
285
 
 
286
 
    my $validParam = 'ea';
287
 
    my $invalidParam = 'inexistent';
288
 
    my $populateOptions = [
289
 
                           { value => 'a' , printableValue => 'a' },
290
 
                           { value => $validParam , printableValue => 'ea' },
291
 
                           { value => 'cas' , printableValue => 'cas' },
292
 
                          ];
293
 
 
294
 
    my $select =  new EBox::Types::Select(
295
 
                                          fieldName => 'select',
296
 
                                          editable  => 1,
297
 
                                          populate => sub { 
298
 
                                              return $populateOptions  
299
 
                                          },
300
 
                                         );
301
 
 
302
 
    lives_ok {
303
 
        $select->setValue($validParam);
304
 
    } 'setting valid parameter';
305
 
    is $select->value(), $validParam,
306
 
        'wether the value has changed';
307
 
    dies_ok {
308
 
        $select->setValue($invalidParam);
309
 
    } 'expecting error when setting value not found in options';
310
 
    isnt $select->value, $invalidParam,
311
 
        'wethr invalid value has not been stored';
312
 
}
313
 
 
314
 
 
315
 
sub defaultValueTest
316
 
{
317
 
    my $defaultValue = 'default';
318
 
    my $populateSub =  sub {
319
 
        return [
320
 
                {  value => 'default', printableValue => 'default' },
321
 
                { value  => 'e', printableValue => 'e',},
322
 
               ]
323
 
    };
324
 
 
325
 
    EBox::Types::Test::defaultValueOk(
326
 
                                      'EBox::Types::Select',
327
 
                                      $defaultValue,
328
 
                                      extraNewParams => [
329
 
                                                         editable => 1,
330
 
                                                         populate => $populateSub
331
 
                                                        ]
332
 
                                     );
333
 
    
334
 
}
335
 
 
336
 
sub storeAndRestoreGConfTest
337
 
{
338
 
 
339
 
    EBox::TestStubs::fakeEBoxModule(name => 'store');
340
 
    
341
 
    my $mod = EBox::Global->modInstance('store');
342
 
    my $dir = 'storeAndRestoreTest';
343
 
 
344
 
    # to remove remains for other tests
345
 
    $mod->delete_dir($dir);
346
 
 
347
 
 
348
 
    my $populateOptions = [
349
 
                           { value => 'a' , printableValue => 'a' },
350
 
                           { value => 'ea' , printableValue => 'ea' },
351
 
                           { value => 'cas' , printableValue => 'cas' },
352
 
                          ];
353
 
 
354
 
    my $select =  new EBox::Types::Select(
355
 
                                          fieldName => 'select',
356
 
                                          editable  => 1,
357
 
                                          populate => sub { 
358
 
                                              return $populateOptions  
359
 
                                          },
360
 
                                         );
361
 
 
362
 
    
363
 
    my @values = map {
364
 
        $_->{value}
365
 
    } @{ $populateOptions };
366
 
    my $otherValue = pop @values;
367
 
 
368
 
    foreach my $value (@values) {
369
 
        $select->setValue($value);
370
 
 
371
 
        lives_ok {
372
 
            $select->storeInGConf($mod, $dir);
373
 
        } "storing in GConf select with value $value";
374
 
        
375
 
 
376
 
        try {
377
 
            $select->setValue($otherValue);
378
 
        }
379
 
        otherwise {
380
 
            my $ex = shift;
381
 
            die "Cannot set value $value: $ex";
382
 
        };
383
 
 
384
 
        my $hash = $mod->hash_from_dir($dir);
385
 
        lives_ok {
386
 
            $select->restoreFromHash($hash);
387
 
        } 'restoring form hash returned by hash_from_dir';
388
 
 
389
 
        is $select->value(), $value,
390
 
            'checking that the value was restored';
391
 
        
392
 
    }
393
 
}
394
 
 
395
 
 
396
 
 
397
 
EBox::TestStubs::activateTestStubs();
398
 
creationTest();
399
 
optionsFromPopulateTest();
400
 
optionsFromForeignModelTest();
401
 
setValueTest();
402
 
storeAndRestoreGConfTest();
403
 
defaultValueTest();
404
 
 
405
 
 
406
 
 
407
 
1;