| 1 | #!/usr/bin/perl |
|---|
| 2 | # $Id: 04-config.t 2562 2008-06-12 05:12:23Z bchoate $ |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use lib 'lib'; |
|---|
| 7 | use lib 'extlib'; |
|---|
| 8 | use lib 't/lib'; |
|---|
| 9 | |
|---|
| 10 | use MT::Test; |
|---|
| 11 | |
|---|
| 12 | use Cwd; |
|---|
| 13 | use File::Spec; |
|---|
| 14 | use File::Temp qw( tempfile ); |
|---|
| 15 | use Test::More tests => 16; |
|---|
| 16 | |
|---|
| 17 | use MT; |
|---|
| 18 | use MT::ConfigMgr; |
|---|
| 19 | |
|---|
| 20 | use vars qw( $BASE ); |
|---|
| 21 | require 't/test-common.pl'; |
|---|
| 22 | |
|---|
| 23 | my($cfg_file, $cfg, $mt); |
|---|
| 24 | |
|---|
| 25 | my $db_dir = cwd() . '/t/db/'; |
|---|
| 26 | (my($fh), $cfg_file) = tempfile(); |
|---|
| 27 | print $fh <<CFG; |
|---|
| 28 | Database $db_dir/mt.db |
|---|
| 29 | ObjectDriver DBI::SQLite |
|---|
| 30 | AltTemplate foo bar |
|---|
| 31 | AltTemplate baz quux |
|---|
| 32 | CFG |
|---|
| 33 | close $fh; |
|---|
| 34 | |
|---|
| 35 | $cfg = MT->config; |
|---|
| 36 | isa_ok($cfg, 'MT::ConfigMgr'); |
|---|
| 37 | ok( $cfg->read_config($cfg_file), "read '$cfg_file'" ); |
|---|
| 38 | |
|---|
| 39 | ## Test standard get/set |
|---|
| 40 | is($cfg->get('Database'), $db_dir . '/mt.db', "get(DataSource)=$db_dir"); |
|---|
| 41 | $cfg->set('DataSource', './db2'); |
|---|
| 42 | is($cfg->get('DataSource'), './db2', 'get(DataSource)=./db2'); |
|---|
| 43 | |
|---|
| 44 | ## Test autoloaded methods |
|---|
| 45 | is($cfg->DataSource, './db2', 'autoloaded DataSource=./db2'); |
|---|
| 46 | $cfg->DataSource('./db'); |
|---|
| 47 | is($cfg->DataSource, './db', 'autoloaded DataSource=./db2'); |
|---|
| 48 | |
|---|
| 49 | ## Test defaults |
|---|
| 50 | is($cfg->Serializer, 'MT', 'Serializer=MT'); |
|---|
| 51 | is($cfg->TimeOffset, 0, 'TimeOffset=0'); |
|---|
| 52 | |
|---|
| 53 | ## Test that multiple settings (AltTemplate) work. |
|---|
| 54 | my @paths = $cfg->AltTemplate; |
|---|
| 55 | is($cfg->type('AltTemplate'), 'ARRAY', 'AltTemplate=ARRAY'); |
|---|
| 56 | is(@paths, 2, 'paths=2'); |
|---|
| 57 | is(($cfg->AltTemplate)[0], 'foo bar', 'foo bar'); |
|---|
| 58 | is(($cfg->AltTemplate)[1], 'baz quux', 'baz quux'); |
|---|
| 59 | |
|---|
| 60 | ## Test bug in early version of ConfigMgr where space was not |
|---|
| 61 | ## stripped from the ends of values |
|---|
| 62 | is($cfg->ObjectDriver, 'DBI::SQLite', 'ObjectDriver=SQLite'); |
|---|
| 63 | |
|---|
| 64 | mkdir $db_dir; |
|---|
| 65 | |
|---|
| 66 | undef $MT::ConfigMgr::cfg; |
|---|
| 67 | ## Test that config file gets read correctly when passed to |
|---|
| 68 | ## constructor. |
|---|
| 69 | $mt = MT->new( Config => $cfg_file, Directory => "." ) or die MT->errstr; |
|---|
| 70 | if (!$mt) { print "# MT constructor returned error: ", MT->errstr(); } |
|---|
| 71 | isa_ok($mt, 'MT'); |
|---|
| 72 | isa_ok($mt->{cfg}, 'MT::ConfigMgr'); |
|---|
| 73 | is($mt->{cfg}->Database, $db_dir . '/mt.db', "DataSource=$db_dir"); |
|---|
| 74 | |
|---|
| 75 | unlink $cfg_file or die "Can't unlink '$cfg_file': $!"; |
|---|