UtilStringTest.php 4.98 KB
Newer Older
Scott Vivian committed
1
<?php
Scott committed
2
require_once QA_INCLUDE_DIR.'util/string.php';
Scott Vivian committed
3 4 5 6

class UtilStringTest extends PHPUnit_Framework_TestCase
{
	private $strBasic = 'So I tied an onion to my belt, which was the style at the time.';
Scott committed
7
	private $strAccents = 'Țĥé qũīçĶ ßřǭŴƞ Ƒöŧ ǰÙƢƥş ØƯĘŕ ƬĦȨ ĿÆƶȳ Ƌơǥ';
Scott Vivian committed
8 9
	private $blockWordString = 't*d o*n b*t style';

10
	public function test__qa_string_to_words()
Scott Vivian committed
11 12 13 14 15 16 17 18 19 20 21
	{
		$test1 = qa_string_to_words($this->strBasic);
		$expected1 = array('so', 'i', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time');

		$test2 = qa_string_to_words($this->strBasic, false);
		$expected2 = array('So', 'I', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time');

		$this->assertEquals($expected1, $test1);
		$this->assertEquals($expected2, $test2);
	}

22
	public function test__qa_string_remove_accents()
Scott Vivian committed
23 24
	{
		$test = qa_string_remove_accents($this->strAccents);
Scott committed
25
		$expected = 'The quicK ssroWn Fot jUOIps OUEr THE LAEzy Dog';
Scott Vivian committed
26 27 28 29

		$this->assertEquals($expected, $test);
	}

Scott committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	public function test__qa_slugify()
	{

		$title1 = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?';
		$title2 = 'Țĥé qũīçĶ ßřǭŴƞ Ƒöŧ ǰÙƢƥş ØƯĘŕ ƬĦȨ ĿÆƶȳ Ƌơǥ';

		$expected1 = 'how-much-wood-would-a-woodchuck-chuck-if-a-woodchuck-could-chuck-wood';
		$expected2 = 'much-wood-would-woodchuck-chuck-woodchuck-could-chuck-wood';
		$expected3 = 'țĥé-qũīçķ-ßřǭŵƞ-ƒöŧ-ǰùƣƥş-øưęŕ-ƭħȩ-ŀæƶȳ-ƌơǥ';
		$expected4 = 'the-quick-ssrown-fot-juoips-ouer-the-laezy-dog';

		$this->assertSame($expected1, qa_slugify($title1));
		$this->assertSame($expected2, qa_slugify($title1, true, 50));
		$this->assertSame($expected3, qa_slugify($title2, false));
		$this->assertSame($expected4, qa_slugify($title2, true));
	}

47
	public function test__qa_tags_to_tagstring()
Scott Vivian committed
48 49 50 51 52 53 54
	{
		$test = qa_tags_to_tagstring( array('Hello', 'World') );
		$expected = 'Hello,World';

		$this->assertEquals($expected, $test);
	}

55
	public function test__qa_tagstring_to_tags()
Scott Vivian committed
56 57 58 59 60 61 62
	{
		$test = qa_tagstring_to_tags('hello,world');
		$expected = array('hello', 'world');

		$this->assertEquals($expected, $test);
	}

63
	public function test__qa_shorten_string_line()
Scott Vivian committed
64 65 66 67 68 69 70 71 72 73
	{
		// qa_shorten_string_line ($string, $length)

		$test = qa_shorten_string_line($this->strBasic, 30);

		$this->assertStringStartsWith('So I tied', $test);
		$this->assertStringEndsWith('time.', $test);
		$this->assertNotFalse(strpos($test, '...'));
	}

74
	public function test__qa_block_words_explode()
Scott Vivian committed
75 76 77 78 79 80 81
	{
		$test = qa_block_words_explode($this->blockWordString);
		$expected = array('t*d', 'o*n', 'b*t', 'style');

		$this->assertEquals($expected, $test);
	}

82
	public function test__qa_block_words_to_preg()
Scott Vivian committed
83 84 85 86 87 88 89
	{
		$test = qa_block_words_to_preg($this->blockWordString);
		$expected = '(?<= )t[^ ]*d(?= )|(?<= )o[^ ]*n(?= )|(?<= )b[^ ]*t(?= )|(?<= )style(?= )';

		$this->assertEquals($expected, $test);
	}

90
	public function test__qa_block_words_match_all()
Scott Vivian committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	{
		$test1 = qa_block_words_match_all('onion belt', '');

		$wordpreg = qa_block_words_to_preg($this->blockWordString);
		$test2 = qa_block_words_match_all('tried an ocean boat', $wordpreg);
		// matches are returned as array of [offset] => [length]
		$expected = array(
			 0 => 5, // tried
			 9 => 5, // ocean
			15 => 4, // boat
		);

		$this->assertEmpty($test1);
		$this->assertEquals($expected, $test2);
	}

107
	public function test__qa_block_words_replace()
Scott Vivian committed
108 109 110 111 112 113 114 115
	{
		$wordpreg = qa_block_words_to_preg($this->blockWordString);
		$test = qa_block_words_replace('tired of my ocean boat style', $wordpreg);
		$expected = '***** of my ***** **** *****';

		$this->assertEquals($expected, $test);
	}

116
	public function test__qa_random_alphanum()
Scott Vivian committed
117 118 119 120 121 122 123
	{
		$len = 50;
		$test = qa_random_alphanum($len);

		$this->assertEquals(strlen($test), $len);
	}

124
	public function test__qa_email_validate()
Scott Vivian committed
125 126 127 128
	{
		$goodEmails = array(
			'hello@example.com',
			'q.a@question2answer.org',
129
			'example@newdomain.app',
Scott Vivian committed
130 131 132 133 134 135
		);
		$badEmails = array(
			'nobody@nowhere',
			'pokémon@example.com',
			'email @ with spaces',
			'some random string',
136
			'dotbeforeat.@email.com',
Scott Vivian committed
137 138 139 140 141 142 143 144 145
		);

		foreach ($goodEmails as $email) {
			$this->assertTrue( qa_email_validate($email) );
		}
		foreach ($badEmails as $email)
			$this->assertFalse( qa_email_validate($email) );
	}

146
	public function test__qa_strlen()
Scott Vivian committed
147 148 149 150 151 152
	{
		$test = qa_strlen($this->strAccents);

		$this->assertEquals($test, 43);
	}

153
	public function test__qa_strtolower()
Scott Vivian committed
154 155 156 157 158 159
	{
		$test = qa_strtolower('hElLo WoRld');

		$this->assertEquals($test, 'hello world');
	}

160
	public function test__qa_substr()
Scott Vivian committed
161 162 163 164 165 166
	{
		$test = qa_substr($this->strBasic, 5, 24);

		$this->assertEquals($test, 'tied an onion to my belt');
	}

167
	public function test__qa_string_matches_one()
Scott Vivian committed
168 169 170 171 172 173 174 175
	{
		$matches = array( 'dyed', 'shallot', 'belt', 'fashion' );
		$nonMatches = array( 'dyed', 'shallot', 'buckle', 'fashion' );

		$this->assertTrue( qa_string_matches_one($this->strBasic, $matches) );
		$this->assertFalse( qa_string_matches_one($this->strBasic, $nonMatches) );
	}
}