summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/I18N/testMessageFormat_gettext.php
blob: 6fa00e802067b84ef220135cb7a3684ade53230e (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

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

class testMessageFormat_gettext extends UnitTestCase
{
	protected $type = 'gettext';
	protected $source;
	protected $tmp;
	protected $dir;
		
	function testMessageFormat_gettext()
	{
		$this->UnitTestCase();
		$this->dir = dirname(__FILE__);
		$this->tmp = $this->dir.'/tmp/';
		$this->source = $this->dir.'/messages';
	}
	
	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();
	}
	
	function testSaveUpdateDelete()
	{
		$MObackup = $this->dir.'/messages/messages.en_AU.mo.bak';
		$MOfile = $this->dir.'/messages/messages.en_AU.mo';
		$PObackup = $this->dir.'/messages/messages.en_AU.po.bak';
		$POfile = $this->dir.'/messages/messages.en_AU.po';		
		
		//restore using the back file
		copy($MObackup,$MOfile);
		copy($PObackup,$POfile);
		
		//test that the back file doesn't contain the 'Testing123' string.
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($MOfile));		
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($POfile));	
		
		$source = MessageSource::factory($this->type, $this->source);
		$source->setCulture('en_AU');
		$source->setCache(new MessageCache($this->tmp));
		
		$formatter = new MessageFormat($source);
		
		//add a untranslated string
		$this->assertEqual($formatter->format('Testing123'), 'Testing123');

		//save it
		$this->assertTrue($formatter->getSource()->save());
		
		//check the contents
		//$this->assertWantedPattern('/Testing123/',file_get_contents($MOfile));
		$this->assertWantedPattern('/Testing123/',file_get_contents($POfile));		
		
		//testing for update.		
		$this->assertTrue($formatter->getSource()->update(
						'Testing123', '123Test', 'update comments'));
						
		$this->assertWantedPattern('/123Test/',file_get_contents($MOfile));			
		
		
		//var_dump(htmlspecialchars($contents));
				
		//now doing some delete		
		$this->assertFalse($formatter->getSource()->delete('Test123'));
		$this->assertTrue($formatter->getSource()->delete('Testing123'));
		
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($MOfile));	
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($POfile));	
		
		//restore using the backup file.
		copy($MObackup,$MOfile);
		copy($PObackup,$POfile);

		$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();
	}
	
	function testDirectoryTypeSaveUpdateDelete()
	{
		$MObackup = $this->dir.'/messages/en_AU/tests.mo.bak';
		$MOfile = $this->dir.'/messages/en_AU/tests.mo';
		$PObackup = $this->dir.'/messages/en_AU/tests.po.bak';
		$POfile = $this->dir.'/messages/en_AU/tests.po';		
		
		//restore using the back file
		copy($MObackup,$MOfile);
		copy($PObackup,$POfile);
		
		//test that the back file doesn't contain the 'Testing123' string.
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($MOfile));		
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($POfile));		
		
		$source = MessageSource::factory($this->type, $this->source);
		$source->setCulture('en_AU');
		$source->setCache(new MessageCache($this->tmp));
		
		$formatter = new MessageFormat($source);

		//add a untranslated string, note, doesn't matter which catalogue
		$this->assertEqual($formatter->format('Testing123'), 'Testing123');
		
		//save it to the 'tests' catalgoue
		$this->assertTrue($formatter->getSource()->save('tests'));
		
		//check the contents
		//$this->assertWantedPattern('/Testing123/',file_get_contents($MOfile));
		$this->assertWantedPattern('/Testing123/',file_get_contents($POfile));		
		
		//testing for update. Update it to the 'tests' catalogue	
		$this->assertTrue($formatter->getSource()->update(
						'Testing123', '123Test', 'update comments','tests'));
						
		$this->assertWantedPattern('/123Test/',file_get_contents($MOfile));					
		
		//now doing some delete	from the 'tests' catalogue
		$this->assertFalse($formatter->getSource()->delete('Test123','tests'));
		$this->assertTrue($formatter->getSource()->delete('Testing123','tests'));
		
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($MOfile));	
		$this->assertNoUnwantedPattern('/Testing123/',file_get_contents($POfile));
		
		//restore using the backup file.
		copy($MObackup,$MOfile);
		copy($PObackup,$POfile);	

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

?>