Mercurial > hg > rc1
comparison vendor/pear/net_sieve/tests/SieveTest.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:50:29 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1e000243b222 |
|---|---|
| 1 <?php | |
| 2 /** | |
| 3 * This file contains the PHPUnit test case for Net_Sieve. | |
| 4 * | |
| 5 * PHP version 5 | |
| 6 * | |
| 7 * +-----------------------------------------------------------------------+ | |
| 8 * | All rights reserved. | | |
| 9 * | | | |
| 10 * | Redistribution and use in source and binary forms, with or without | | |
| 11 * | modification, are permitted provided that the following conditions | | |
| 12 * | are met: | | |
| 13 * | | | |
| 14 * | o Redistributions of source code must retain the above copyright | | |
| 15 * | notice, this list of conditions and the following disclaimer. | | |
| 16 * | o Redistributions in binary form must reproduce the above copyright | | |
| 17 * | notice, this list of conditions and the following disclaimer in the | | |
| 18 * | documentation and/or other materials provided with the distribution.| | |
| 19 * | | | |
| 20 * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | | |
| 21 * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | | |
| 22 * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | | |
| 23 * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | | |
| 24 * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | | |
| 25 * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | | |
| 26 * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | |
| 27 * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | |
| 28 * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | |
| 29 * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | | |
| 30 * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | |
| 31 * +-----------------------------------------------------------------------+ | |
| 32 * | |
| 33 * @category Networking | |
| 34 * @package Net_Sieve | |
| 35 * @author Anish Mistry <amistry@am-productions.biz> | |
| 36 * @copyright 2006 Anish Mistry | |
| 37 * @license http://www.opensource.org/licenses/bsd-license.php BSD | |
| 38 * @version SVN: $Id$ | |
| 39 * @link http://pear.php.net/package/Net_Sieve | |
| 40 */ | |
| 41 | |
| 42 require_once dirname(__FILE__) . '/../Sieve.php'; | |
| 43 | |
| 44 /** | |
| 45 * PHPUnit test case for Net_Sieve. | |
| 46 * | |
| 47 * @category Networking | |
| 48 * @package Net_Sieve | |
| 49 * @author Anish Mistry <amistry@am-productions.biz> | |
| 50 * @copyright 2006 Anish Mistry | |
| 51 * @license http://www.opensource.org/licenses/bsd-license.php BSD | |
| 52 * @version Release: @package_version@ | |
| 53 * @link http://pear.php.net/package/Net_Sieve | |
| 54 */ | |
| 55 class SieveTest extends PHPUnit_Framework_TestCase | |
| 56 { | |
| 57 // contains the object handle of the string class | |
| 58 protected $fixture; | |
| 59 | |
| 60 protected function setUp() | |
| 61 { | |
| 62 if (!file_exists(dirname(__FILE__) . '/config.php')) { | |
| 63 $this->markTestSkipped('Test configuration incomplete. Copy config.php.dist to config.php.'); | |
| 64 } | |
| 65 require_once dirname(__FILE__) . '/config.php'; | |
| 66 | |
| 67 // Create a new instance of Net_Sieve. | |
| 68 $this->_pear = new PEAR(); | |
| 69 $this->fixture = new Net_Sieve(); | |
| 70 $this->scripts = array( | |
| 71 'test script1' => "require \"fileinto\";\r\nif header :contains \"From\" \"@cnba.uba.ar\" \r\n{fileinto \"INBOX.Test1\";}\r\nelse \r\n{fileinto \"INBOX\";}", | |
| 72 'test script2' => "require \"fileinto\";\r\nif header :contains \"From\" \"@cnba.uba.ar\" \r\n{fileinto \"INBOX.Test\";}\r\nelse \r\n{fileinto \"INBOX\";}", | |
| 73 'test"scriptäöü3' => "require \"vacation\";\nvacation\n:days 7\n:addresses [\"matthew@de-construct.com\"]\n:subject \"This is a test\"\n\"I'm on my holiday!\nsadfafs\";", | |
| 74 'test script4' => file_get_contents(dirname(__FILE__) . '/largescript.siv')); | |
| 75 } | |
| 76 | |
| 77 protected function tearDown() | |
| 78 { | |
| 79 // Delete the instance. | |
| 80 unset($this->fixture); | |
| 81 } | |
| 82 | |
| 83 protected function login() | |
| 84 { | |
| 85 $result = $this->fixture->connect(HOST, PORT); | |
| 86 $this->assertTrue($this->check($result), 'Can not connect'); | |
| 87 $result = $this->fixture->login(USERNAME, PASSWORD, null, '', false); | |
| 88 $this->assertTrue($this->check($result), 'Can not login'); | |
| 89 } | |
| 90 | |
| 91 protected function logout() | |
| 92 { | |
| 93 $result = $this->fixture->disconnect(); | |
| 94 $this->assertFalse($this->_pear->isError($result), 'Error on disconnect'); | |
| 95 } | |
| 96 | |
| 97 protected function clear() | |
| 98 { | |
| 99 // Clear all the scripts in the account. | |
| 100 $this->login(); | |
| 101 $active = $this->fixture->getActive(); | |
| 102 if (isset($this->scripts[$active])) { | |
| 103 $this->fixture->setActive(null); | |
| 104 } | |
| 105 foreach (array_keys($this->scripts) as $script) { | |
| 106 $this->fixture->removeScript($script); | |
| 107 } | |
| 108 $this->logout(); | |
| 109 } | |
| 110 | |
| 111 protected function check($result) | |
| 112 { | |
| 113 if ($this->_pear->isError($result)) { | |
| 114 throw new Exception($result->getMessage()); | |
| 115 } | |
| 116 return $result; | |
| 117 } | |
| 118 | |
| 119 public function testConnect() | |
| 120 { | |
| 121 $result = $this->fixture->connect(HOST, PORT); | |
| 122 $this->assertTrue($this->check($result), 'Cannot connect'); | |
| 123 } | |
| 124 | |
| 125 public function testLogin() | |
| 126 { | |
| 127 $result = $this->fixture->connect(HOST, PORT); | |
| 128 $this->assertTrue($this->check($result), 'Cannot connect'); | |
| 129 $result = $this->fixture->login(USERNAME, PASSWORD, null, '', false); | |
| 130 $this->assertTrue($this->check($result), 'Cannot login'); | |
| 131 } | |
| 132 | |
| 133 public function testDisconnect() | |
| 134 { | |
| 135 $result = $this->fixture->connect(HOST, PORT); | |
| 136 $this->assertFalse($this->_pear->isError($result), 'Cannot connect'); | |
| 137 $result = $this->fixture->login(USERNAME, PASSWORD, null, '', false); | |
| 138 $this->assertFalse($this->_pear->isError($result), 'Cannot login'); | |
| 139 $result = $this->fixture->disconnect(); | |
| 140 $this->assertFalse($this->_pear->isError($result), 'Error on disconnect'); | |
| 141 } | |
| 142 | |
| 143 public function testListScripts() | |
| 144 { | |
| 145 $this->login(); | |
| 146 $scripts = $this->fixture->listScripts(); | |
| 147 $this->logout(); | |
| 148 $this->assertFalse($this->_pear->isError($scripts), 'Can not list scripts'); | |
| 149 } | |
| 150 | |
| 151 public function testInstallScript() | |
| 152 { | |
| 153 $this->clear(); | |
| 154 $this->login(); | |
| 155 | |
| 156 // First script. | |
| 157 $scriptname = 'test script1'; | |
| 158 $before_scripts = $this->fixture->listScripts(); | |
| 159 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 160 $this->assertFalse($this->_pear->isError($result), 'Can not install script ' . $scriptname); | |
| 161 $after_scripts = $this->fixture->listScripts(); | |
| 162 $diff_scripts = array_values(array_diff($after_scripts, $before_scripts)); | |
| 163 $this->assertTrue(count($diff_scripts) > 0, 'Script not installed'); | |
| 164 $this->assertEquals($scriptname, $diff_scripts[0], 'Added script has a different name'); | |
| 165 | |
| 166 // Second script (install and activate) | |
| 167 $scriptname = 'test script2'; | |
| 168 $before_scripts = $this->fixture->listScripts(); | |
| 169 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname], true); | |
| 170 $this->assertFalse($this->_pear->isError($result), 'Can not install script ' . $scriptname); | |
| 171 $after_scripts = $this->fixture->listScripts(); | |
| 172 $diff_scripts = array_values(array_diff($after_scripts, $before_scripts)); | |
| 173 $this->assertTrue(count($diff_scripts) > 0, 'Script not installed'); | |
| 174 $this->assertEquals($scriptname, $diff_scripts[0], 'Added script has a different name'); | |
| 175 $active_script = $this->fixture->getActive(); | |
| 176 $this->assertEquals($scriptname, $active_script, 'Added script has a different name'); | |
| 177 $this->logout(); | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * There is a good chance that this test will fail since most servers have | |
| 182 * a 32KB limit on uploaded scripts. | |
| 183 */ | |
| 184 public function testInstallScriptLarge() | |
| 185 { | |
| 186 $this->clear(); | |
| 187 $this->login(); | |
| 188 $scriptname = 'test script4'; | |
| 189 $before_scripts = $this->fixture->listScripts(); | |
| 190 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 191 $this->assertFalse($this->_pear->isError($result), 'Unable to upload large script (expected behavior for most servers)'); | |
| 192 $after_scripts = $this->fixture->listScripts(); | |
| 193 $diff_scripts = array_diff($after_scripts, $before_scripts); | |
| 194 $this->assertEquals($scriptname, reset($diff_scripts), 'Added script has a different name'); | |
| 195 $this->logout(); | |
| 196 } | |
| 197 | |
| 198 /** | |
| 199 * See bug #16691. | |
| 200 */ | |
| 201 public function testInstallNonAsciiScript() | |
| 202 { | |
| 203 $this->clear(); | |
| 204 $this->login(); | |
| 205 | |
| 206 $scriptname = 'test"scriptäöü3'; | |
| 207 $before_scripts = $this->fixture->listScripts(); | |
| 208 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 209 $this->assertFalse($this->_pear->isError($result), 'Can not install script ' . $scriptname); | |
| 210 $after_scripts = $this->fixture->listScripts(); | |
| 211 $diff_scripts = array_values(array_diff($after_scripts, $before_scripts)); | |
| 212 $this->assertTrue(count($diff_scripts) > 0, 'Script not installed'); | |
| 213 $this->assertEquals($scriptname, $diff_scripts[0], 'Added script has a different name'); | |
| 214 | |
| 215 $this->logout(); | |
| 216 } | |
| 217 | |
| 218 public function testGetScript() | |
| 219 { | |
| 220 $this->clear(); | |
| 221 $this->login(); | |
| 222 $scriptname = 'test script1'; | |
| 223 $before_scripts = $this->fixture->listScripts(); | |
| 224 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 225 $this->assertFalse($this->_pear->isError($result), 'Can not install script ' . $scriptname); | |
| 226 $after_scripts = $this->fixture->listScripts(); | |
| 227 $diff_scripts = array_values(array_diff($after_scripts, $before_scripts)); | |
| 228 $this->assertTrue(count($diff_scripts) > 0); | |
| 229 $this->assertEquals($scriptname, $diff_scripts[0], 'Added script has a different name'); | |
| 230 $script = $this->fixture->getScript($scriptname); | |
| 231 $this->assertEquals(trim($this->scripts[$scriptname]), trim($script), 'Script installed is not the same script retrieved'); | |
| 232 $this->logout(); | |
| 233 } | |
| 234 | |
| 235 public function testGetActive() | |
| 236 { | |
| 237 $this->clear(); | |
| 238 $this->login(); | |
| 239 $active_script = $this->fixture->getActive(); | |
| 240 $this->assertFalse($this->_pear->isError($active_script), 'Error getting the active script'); | |
| 241 $this->logout(); | |
| 242 } | |
| 243 | |
| 244 public function testSetActive() | |
| 245 { | |
| 246 $this->clear(); | |
| 247 $scriptname = 'test script1'; | |
| 248 $this->login(); | |
| 249 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 250 $result = $this->fixture->setActive($scriptname); | |
| 251 $this->assertFalse($this->_pear->isError($result), 'Can not set active script'); | |
| 252 $active_script = $this->fixture->getActive(); | |
| 253 $this->assertEquals($scriptname, $active_script, 'Active script does not match'); | |
| 254 | |
| 255 // Test for non-existant script. | |
| 256 $result = $this->fixture->setActive('non existant script'); | |
| 257 $this->assertTrue($this->_pear->isError($result)); | |
| 258 $this->logout(); | |
| 259 } | |
| 260 | |
| 261 public function testRemoveScript() | |
| 262 { | |
| 263 $this->clear(); | |
| 264 $scriptname = 'test script1'; | |
| 265 $this->login(); | |
| 266 $result = $this->fixture->installScript($scriptname, $this->scripts[$scriptname]); | |
| 267 $result = $this->fixture->removeScript($scriptname); | |
| 268 $this->assertFalse($this->_pear->isError($result), 'Error removing active script'); | |
| 269 $this->logout(); | |
| 270 } | |
| 271 } |
