summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/I18N/testMessageFormat_SQLite.php
blob: 5b75a0a7a41c173ffd6d9329889bdf883bf355b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

Prado::using('System.I18N.core.MessageFormat');

class testMessageFormat_SQLite extends UnitTestCase
{
	
	protected $type = 'SQLite';
	protected $source = 'sqlite:///./messages/messages.db';
	protected $tmp;
	protected $dir;
	
	function testMessageFormat_SQLite()
	{
		$this->UnitTestCase();
		$this->dir = dirname(__FILE__);
		$this->tmp = $this->dir.'/tmp/';
		$this->source = "sqlite:///{$this->dir}/messages/messages.db";

	}
	
	function test1()
	{
		$source = MessageSource::factory($this->type, $this->source);
		$source->setCulture('en_AU');
		$source->setCache(new MessageCache($this->tmp));

		$formatter = new MessageFormat($source);
		$this->assertEqual($formatter->format('Hello'),'G\'day Mate!');
	
		$this->assertEqual($formatter->format('Goodbye'), 'Goodbye');
		
		$formatter->setUntranslatedPS(array('[T]','[/T]'));	
		$this->assertEqual($formatter->format('Hi'), '[T]Hi[/T]');
		
		$source->getCache()->clear();

		//save the untranslated
		
	}
	
	function getAllContents($file)
	{
		$db = sqlite_open($file);
		$rs = sqlite_query('SELECT * FROM trans_unit',$db);
		$result = '';
		while($row = sqlite_fetch_array($rs,SQLITE_NUM))
		{
			$result .= implode(', ',$row)."\n";
		}
		sqlite_close($db);
		return $result;
	}
	
	function testSaveUpdateDelete()
	{
		$backup = $this->dir.'/messages/messages.db.bak';
		$dbfile = $this->dir.'/messages/messages.db';
		
		//restore using the back file
		copy($backup,$dbfile);
		
		//test that the back file doesn't contain the 'Testing123' string.
		$contents = $this->getAllContents($dbfile);
		$this->assertNoUnwantedPattern('/Testing123/',$contents);
		
		$source = MessageSource::factory($this->type, $this->source);
		$source->setCulture('en_AU');
		$source->setCache(new MessageCache($this->tmp));
		
		$formatter = new MessageFormat($source);
		
		$formatter->setUntranslatedPS(array('[t]','[/t]'));
		
		//add a untranslated string
		$this->assertEqual($formatter->format('Testing123'), '[t]Testing123[/t]');

		//save it
		$this->assertTrue($formatter->getSource()->save());
		
		//check the contents
		$contents = $this->getAllContents($dbfile);
		$this->assertWantedPattern('/Testing123/',$contents);
		
		//testing for update.		
		$this->assertTrue($formatter->getSource()->update(
						'Testing123', '123Test', 'update comments'));
						
		$contents = $this->getAllContents($dbfile);		
		$this->assertWantedPattern('/123Test/',$contents);			
		$this->assertWantedPattern('/update comments/',$contents);					
		
		//var_dump(htmlspecialchars($contents));
				
		//now doing some delete		
		//doesn't detect missing source
		$this->assertTrue($formatter->getSource()->delete('Test123'));
		$this->assertTrue($formatter->getSource()->delete('Testing123'));
		
		$contents = $this->getAllContents($dbfile);		
		$this->assertNoUnwantedPattern('/Testing123/',$contents);	
		
		//restore using the backup file.
		copy($backup,$dbfile);
		$source->getCache()->clear();
	}
	
	function testCatalogueList()
	{
		$source = MessageSource::factory($this->type, $this->source);
		$result[] = array('messages',NULL);
		$result[] = array('messages', 'en');
		$result[] = array('messages','en_AU');
		$result[] = array('tests',NULL);
		$result[] = array('tests','en');
		$result[] = array('tests','en_AU');

		$this->assertEqual($result, $source->catalogues());
	}
	
	function testAltCatalogue()
	{
		$source = MessageSource::factory($this->type, $this->source);
		$source->setCulture('en_AU');
		$source->setCache(new MessageCache($this->tmp));	
		
		$formatter = new MessageFormat($source);
		$formatter->Catalogue = 'tests';
		
		//from a different catalogue
		$this->assertEqual($formatter->format('Hello'), 'Howdy!');	
		$this->assertEqual($formatter->format('Welcome'), 'Ho Ho!');	
		$this->assertEqual($formatter->format('Goodbye'), 'Sayonara');	
		
		//switch to 'messages' catalogue
		$this->assertEqual($formatter->format('Hello',null,'messages'),'G\'day Mate!');

		$source->getCache()->clear();
	}
}

?>