aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utf/utf8_wordwrap_test.php
blob: fbc947b92a7e968c61cd6e9e7f0fbd310218cb8a (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
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';

class phpbb_utf_utf8_wordwrap_test extends phpbb_test_case
{
	public function test_utf8_wordwrap_ascii()
	{
		// if the input is all ascii it should work exactly like php's wordwrap

		$text = 'The quick brown fox jumped over the lazy dog.';

		$php_wordwrap = wordwrap($text, 20);
		$phpbb_utf8_wordwrap = utf8_wordwrap($text, 20);
		$this->assertEquals($php_wordwrap, $phpbb_utf8_wordwrap, "Checking ASCII standard behaviour with length 20");

		$php_wordwrap = wordwrap($text, 30, "<br />\n");
		$phpbb_utf8_wordwrap = utf8_wordwrap($text, 30, "<br />\n");
		$this->assertEquals($php_wordwrap, $phpbb_utf8_wordwrap, "Checking ASCII special break string with length 30");

		$text = 'A very long woooooooooooord.';

		$php_wordwrap = wordwrap($text, 8, "\n");
		$phpbb_utf8_wordwrap = utf8_wordwrap($text, 8, "\n");
		$this->assertEquals($php_wordwrap, $phpbb_utf8_wordwrap, 'Checking ASCII not cutting long words');

		$php_wordwrap = wordwrap($text, 8, "\n", true);
		$phpbb_utf8_wordwrap = utf8_wordwrap($text, 8, "\n", true);
		$this->assertEquals($php_wordwrap, $phpbb_utf8_wordwrap, 'Checking ASCII cutting long words');
	}

	/**
	* Helper function that generates meaningless greek text
	*/
	private function turn_into_greek($string)
	{
		$greek_chars = array("\xCE\x90", "\xCE\x91", "\xCE\x92", "\xCE\x93", "\xCE\x94", "\xCE\x95", "\xCE\x96", "\xCE\x97", "\xCE\x98", "\xCE\x99");

		$greek = '';
		for ($i = 0, $n = strlen($string); $i < $n; $i++)
		{
			// replace each number with the character from the array
			if (ctype_digit($string[$i]))
			{
				$greek .= $greek_chars[(int) $string[$i]];
			}
			else
			{
				$greek .= $string[$i];
			}
		}

		return $greek;
	}

	public function test_utf8_wordwrap_utf8()
	{
		$text = "0123456 0123 012345 01234";
		$greek = $this->turn_into_greek($text);

		$expected = $this->turn_into_greek(wordwrap($text, 10));
		$phpbb_utf8_wordwrap = utf8_wordwrap($greek, 10);
		$this->assertEquals($expected, $phpbb_utf8_wordwrap, 'Checking UTF-8 standard behaviour with length 10');
	}

	public function test_utf8_wordwrap_utf8_cut()
	{
		$text = "0123456 0123 012345 01234";
		$greek = $this->turn_into_greek($text);

		$expected = $this->turn_into_greek(wordwrap($text, 5, "\n", true));
		$phpbb_utf8_wordwrap = utf8_wordwrap($greek, 5, "\n", true);
		$this->assertEquals($expected, $phpbb_utf8_wordwrap, 'Checking UTF-8 cutting long words');
	}
}