3
* Copyright 2007-2008 Maintainable Software, LLC
4
* Copyright 2008-2011 Horde LLC (http://www.horde.org/)
6
* @author Mike Naberezny <mike@maintainable.com>
7
* @author Derek DeVries <derek@maintainable.com>
8
* @author Chuck Hagenbuch <chuck@horde.org>
9
* @license http://www.horde.org/licenses/bsd
12
* @subpackage UnitTests
17
* @author Mike Naberezny <mike@maintainable.com>
18
* @author Derek DeVries <derek@maintainable.com>
19
* @author Chuck Hagenbuch <chuck@horde.org>
20
* @license http://www.horde.org/licenses/bsd
23
* @subpackage UnitTests
25
class Horde_View_Helper_TextTest extends Horde_Test_Case
27
public function setUp()
29
$this->view = new Horde_View();
30
$this->view->addHelper('Text');
34
public function testEscape()
36
$text = "Test 'escaping html' \"quotes\" and & amps";
37
$expected = "Test 'escaping html' "quotes" and & amps";
38
$this->assertEquals($expected, $this->view->h($text));
42
public function testTruncate()
44
$str = 'The quick brown fox jumps over the lazy dog tomorrow morning.';
45
$expected = 'The quick brown fox jumps over the la...';
46
$this->assertEquals($expected, $this->view->truncate($str, 40));
50
public function testTruncateMiddle()
52
$str = 'The quick brown fox jumps over the lazy dog tomorrow morning.';
53
$expected = 'The quick brown fox... tomorrow morning.';
54
$this->assertEquals($expected, $this->view->truncateMiddle($str, 40));
57
// text too short to trucate
58
public function testTruncateMiddleTooShort()
60
$str = 'The quick brown fox jumps over the dog.';
61
$expected = 'The quick brown fox jumps over the dog.';
62
$this->assertEquals($expected, $this->view->truncateMiddle($str, 40));
67
public function testHighlightDefault()
69
$str = 'The quick brown fox jumps over the dog.';
70
$expected = 'The quick <strong class="highlight">brown</strong> fox jumps over the dog.';
71
$this->assertEquals($expected, $this->view->highlight($str, 'brown'));
74
// test failure to highlight
75
public function testHighlightCustom()
77
$str = 'The quick brown fox jumps over the dog.';
78
$expected = 'The quick <em>brown</em> fox jumps over the dog.';
79
$this->assertEquals($expected, $this->view->highlight($str, 'brown', '<em>$1</em>'));
82
// test failure to highlight
83
public function testHighlightNoMatch()
85
$str = 'The quick brown fox jumps over the dog.';
86
$this->assertEquals($str, $this->view->highlight($str, 'black'));
89
public function testCycleClass()
91
$value = new Horde_View_Helper_Text_Cycle(array('one', 2, '3'));
93
$this->assertEquals('one', (string)$value);
94
$this->assertEquals('2', (string)$value);
95
$this->assertEquals('3', (string)$value);
96
$this->assertEquals('one', (string)$value);
98
$this->assertEquals('one', (string)$value);
99
$this->assertEquals('2', (string)$value);
100
$this->assertEquals('3', (string)$value);
103
public function testCycleClassWithInvalidArguments()
106
$value = new Horde_View_Helper_Text_Cycle('bad');
108
} catch (InvalidArgumentException $e) {}
111
$value = new Horde_View_Helper_Text_Cycle(array('foo'));
113
} catch (InvalidArgumentException $e) {}
116
$value = new Horde_View_Helper_Text_Cycle(array('foo', 'bar'), 'bad-arg');
118
} catch (InvalidArgumentException $e) {}
121
public function testCycleResetsWithNewValues()
123
$this->assertEquals('even', (string)$this->view->cycle('even', 'odd'));
124
$this->assertEquals('odd', (string)$this->view->cycle('even', 'odd'));
125
$this->assertEquals('even', (string)$this->view->cycle('even', 'odd'));
126
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3));
127
$this->assertEquals('2', (string)$this->view->cycle(1, 2, 3));
128
$this->assertEquals('3', (string)$this->view->cycle(1, 2, 3));
131
public function testNamedCycles()
133
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
134
$this->assertEquals('red', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
135
$this->assertEquals('2', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
136
$this->assertEquals('blue', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
137
$this->assertEquals('3', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
138
$this->assertEquals('red', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
141
public function testDefaultNamedCycle()
143
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3));
144
$this->assertEquals('2', (string)$this->view->cycle(1, 2, 3, array('name' => 'default')));
145
$this->assertEquals('3', (string)$this->view->cycle(1, 2, 3));
148
public function testResetCycle()
150
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3));
151
$this->assertEquals('2', (string)$this->view->cycle(1, 2, 3));
152
$this->view->resetCycle();
153
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3));
156
public function testResetUnknownCycle()
158
$this->view->resetCycle('colors');
161
public function testResetNamedCycle()
163
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
164
$this->assertEquals('red', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
165
$this->view->resetCycle('numbers');
166
$this->assertEquals('1', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
167
$this->assertEquals('blue', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
168
$this->assertEquals('2', (string)$this->view->cycle(1, 2, 3, array('name' => 'numbers')));
169
$this->assertEquals('red', (string)$this->view->cycle('red', 'blue', array('name' => 'colors')));
172
public function testPluralization()
174
$this->assertEquals('1 count', $this->view->pluralize(1, 'count'));
175
$this->assertEquals('2 counts', $this->view->pluralize(2, 'count'));
176
$this->assertEquals('1 count', $this->view->pluralize('1', 'count'));
177
$this->assertEquals('2 counts', $this->view->pluralize('2', 'count'));
178
$this->assertEquals('1,066 counts', $this->view->pluralize('1,066', 'count'));
179
$this->assertEquals('1.25 counts', $this->view->pluralize('1.25', 'count'));
180
$this->assertEquals('2 counters', $this->view->pluralize('2', 'count', 'counters'));