1
# Copyright (C) 2008-2012 eBox Technologies S.L.
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.
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.
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
20
use Test::More tests => 20;
31
use EBox::Types::Test;
32
use EBox::Types::Select;
33
use EBox::Types::Text;
34
use EBox::Model::DataTable;
42
my $class = 'EBox::Types::Select';
44
EBox::Types::Test::createOk(
48
fieldName => 'select',
50
populate => sub { return [] },
52
'select with populate option creation ok'
55
EBox::Types::Test::createOk(
59
fieldName => 'select',
61
foreignModel => \&_foreignModel,
62
foreignField => 'name',
64
'select with foreignModel and foreignField options creation ok'
67
EBox::Types::Test::createFail(
71
fieldName => 'select',
72
populate => sub { return [] },
74
'select without editable option fails construction'
78
EBox::Types::Test::createFail(
82
fieldName => 'select',
83
populate => sub { return [] },
87
'select with optional option fails construction'
91
EBox::Types::Test::createFail(
95
fieldName => 'select',
99
'select without either populate or foreignModel fails construction'
102
EBox::Types::Test::createFail(
106
fieldName => 'select',
109
foreignModel => \&_foreignModel,
111
'select with foreignModel but lacking foreignField fails construction'
115
EBox::Types::Test::createFail(
119
fieldName => 'select',
122
foreignField => 'name',
124
'select with foreignField but lacking foreignModel fails construction'
135
if (not defined $_foreignModel) {
136
$_foreignModel = __PACKAGE__->_newPopulatedDataTable();
139
return $_foreignModel;
145
my ($self, $table) = @_;
146
if (not defined $table) {
150
EBox::TestStubs::fakeEBoxModule(name => 'fakeModule');
151
my $gconfmodule = EBox::Global->modInstance('fakeModule');
153
my $dataTableDir = '/ebox/modules/fakeModule/DataTable';
154
# remove old data from prvious modules
155
$gconfmodule->delete_dir($dataTableDir);
158
my $dataTableBase = EBox::Model::DataTable->new(
159
gconfmodule => $gconfmodule,
160
directory => $dataTableDir,
165
my $dataTable = Test::MockObject::Extends->new($dataTableBase);
166
$dataTable->set_always('_table' => $table);
173
sub _newPopulatedDataTable
177
my $tableDescription = _tableDescription4fields();
180
my $dataTable = $self->_newDataTable($tableDescription);
184
[ name => 'a', regularField => 'regular' ],
186
name => 'b', regularField => 'regular',
187
defaultField => 'noDefaultText'
190
name => 'c', regularField => 'regular',
191
optionalField => 'noDefaultText'
196
$dataTable->add( @{ $_ } );
206
sub _tableDescription4fields
208
my $tableDescription = {
209
tableDescription => [
210
new EBox::Types::Text(
212
printableName => 'name',
215
new EBox::Types::Text(
216
fieldName => 'regularField',
217
printableName => 'regularField',
219
new EBox::Types::Text(
220
fieldName => 'defaultField',
221
printableName => 'defaultField',
222
defaultValue => 'defaultText',
225
new EBox::Types::Text(
226
fieldName => 'optionalField',
227
printableName => 'optionalField',
236
return $tableDescription;
240
sub optionsFromPopulateTest
242
my $populateOptions = [
243
{ value => 'a' , printableValue => 'a' },
244
{ value => 'ea' , printableValue => 'ea' },
245
{ value => 'cas' , printableValue => 'cas' },
248
my $select = new EBox::Types::Select(
249
fieldName => 'select',
252
return $populateOptions
256
is_deeply $select->options(), $populateOptions,
257
'checking options returned from a select with populate attribute';
260
sub optionsFromForeignModelTest
262
my $select = new EBox::Types::Select(
263
fieldName => 'select',
265
foreignModel => \&_foreignModel,
266
foreignField => 'name',
271
my $name = $_->valueByName('name');
272
{value => $id, printableValue => $name },
273
} @{ _foreignModel->rows() };
275
my @actual = @{ $select->options() };
278
is_deeply \@actual, \@expected,
279
'checking options returned from a select with foreign model';
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' },
294
my $select = new EBox::Types::Select(
295
fieldName => 'select',
298
return $populateOptions
303
$select->setValue($validParam);
304
} 'setting valid parameter';
305
is $select->value(), $validParam,
306
'wether the value has changed';
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';
317
my $defaultValue = 'default';
318
my $populateSub = sub {
320
{ value => 'default', printableValue => 'default' },
321
{ value => 'e', printableValue => 'e',},
325
EBox::Types::Test::defaultValueOk(
326
'EBox::Types::Select',
330
populate => $populateSub
336
sub storeAndRestoreGConfTest
339
EBox::TestStubs::fakeEBoxModule(name => 'store');
341
my $mod = EBox::Global->modInstance('store');
342
my $dir = 'storeAndRestoreTest';
344
# to remove remains for other tests
345
$mod->delete_dir($dir);
348
my $populateOptions = [
349
{ value => 'a' , printableValue => 'a' },
350
{ value => 'ea' , printableValue => 'ea' },
351
{ value => 'cas' , printableValue => 'cas' },
354
my $select = new EBox::Types::Select(
355
fieldName => 'select',
358
return $populateOptions
365
} @{ $populateOptions };
366
my $otherValue = pop @values;
368
foreach my $value (@values) {
369
$select->setValue($value);
372
$select->storeInGConf($mod, $dir);
373
} "storing in GConf select with value $value";
377
$select->setValue($otherValue);
381
die "Cannot set value $value: $ex";
384
my $hash = $mod->hash_from_dir($dir);
386
$select->restoreFromHash($hash);
387
} 'restoring form hash returned by hash_from_dir';
389
is $select->value(), $value,
390
'checking that the value was restored';
397
EBox::TestStubs::activateTestStubs();
399
optionsFromPopulateTest();
400
optionsFromForeignModelTest();
402
storeAndRestoreGConfTest();