PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Services_Trackback/pear/tests/Services_Trackback/test/Services_Trackback_Test.php

http://nothing-at-all.googlecode.com/
PHP | 448 lines | 350 code | 93 blank | 5 comment | 11 complexity | d2b5f428f11c35e4b59303122639e6e0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once 'Services/Trackback.php';
  3. require_once 'Services/Trackback/SpamCheck.php';
  4. require_once 'Services/Trackback/SpamCheck/Mock.php';
  5. require_once 'PHPUnit/Framework/TestCase.php';
  6. require_once dirname(__FILE__).'/trackback_data.php';
  7. class Services_Trackback_Test extends PHPUnit_Framework_TestCase
  8. {
  9. var $xml;
  10. function setUp() {
  11. $path = dirname(__FILE__) . '/data/Services_Trackback_Test/';
  12. $dir = new DirectoryIterator($path);
  13. $this->xml = array();
  14. foreach ($dir as $file) {
  15. @list($testName, $extension) = explode('.', (string)$file);
  16. if ($extension !== 'xml') {
  17. continue;
  18. }
  19. $full_path = $path . "/" . $file;
  20. $xml = file_get_contents($full_path);
  21. $this->xml[$testName] = trim($xml);
  22. }
  23. }
  24. function testCreate()
  25. {
  26. global $trackbackData;
  27. $options = array(
  28. 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  29. 'timeout' => 10,
  30. 'fetchlines' => 100,
  31. 'fetchextra' => true,
  32. 'httprequest' => array(
  33. 'allowRedirects' => false,
  34. 'maxRedirects' => 0,
  35. 'useragent' => 'Mozilla 10.0',
  36. ),
  37. );
  38. $fakeTrack = new Services_Trackback;
  39. $fakeTrack->_options = $options;
  40. $fakeTrack->_data = $trackbackData['nospam'];
  41. $this->assertTrue(Services_Trackback::create($trackbackData['nospam'], $options) == $fakeTrack);
  42. }
  43. function testSetOptionsSuccess()
  44. {
  45. $options = array(
  46. 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  47. 'timeout' => 10,
  48. 'fetchlines' => 100,
  49. 'fetchextra' => true,
  50. 'httprequest' => array(
  51. 'allowRedirects' => false,
  52. 'maxRedirects' => 0,
  53. 'useragent' => 'Mozilla 10.0',
  54. ),
  55. );
  56. $fakeTrack = new Services_Trackback;
  57. $realTrack = new Services_Trackback;
  58. $fakeTrack->_options = $options;
  59. $realTrack->setOptions($options);
  60. $this->assertTrue($realTrack == $fakeTrack);
  61. }
  62. function testGetOptionsSuccess()
  63. {
  64. $options = array(
  65. 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  66. 'timeout' => 10,
  67. 'fetchlines' => 100,
  68. 'httpRequest' => array(
  69. 'allowRedirects' => false,
  70. 'maxRedirects' => 0,
  71. 'useragent' => 'Mozilla 10.0',
  72. ),
  73. );
  74. $track = new Services_Trackback;
  75. $track->_options = $options;
  76. $this->assertTrue($track->getOptions() == $options);
  77. }
  78. function testAutodiscoverSuccess()
  79. {
  80. $data = array(
  81. 'id' => 'Test',
  82. 'url' => 'http://pear.php.net/package/net_ftp'
  83. );
  84. $track1 = Services_Trackback::create($data);
  85. $track1->autodiscover();
  86. $data['trackback_url'] = 'http://pear.php.net/trackback/trackback.php?id=Net_FTP';
  87. $track2 = Services_Trackback::create($data);
  88. $this->assertTrue($track1 == $track2);
  89. }
  90. function testAutodiscoverFailure()
  91. {
  92. $data = array(
  93. 'id' => 'Test',
  94. 'url' => 'http://pear.php.net/'
  95. );
  96. $track1 = Services_Trackback::create($data);
  97. $res = $track1->autodiscover();
  98. $this->assertTrue(PEAR::isError($res));
  99. }
  100. function testSend()
  101. {
  102. global $trackbackData;
  103. $track = Services_Trackback::create($trackbackData['nospam']);
  104. }
  105. function testGetAutodiscoveryCodeNoComments()
  106. {
  107. global $trackbackData;
  108. $data = $trackbackData['nospam'];
  109. $xml = $this->xml['testGetAutodiscoveryCodeNoComments'];
  110. $xml = sprintf($xml, $data['url'], $data['url'], $data['title'], $data['trackback_url']);
  111. $track = Services_Trackback::create($data);
  112. $result = trim($track->getAutodiscoveryCode(false));
  113. $this->assertSame($xml, $result);
  114. }
  115. function testGetAutodiscoveryCodeComments()
  116. {
  117. global $trackbackData;
  118. $data = $trackbackData['nospam'];
  119. $xml = $this->xml['testGetAutodiscoveryCodeComments'];
  120. $xml = sprintf($xml, $data['url'], $data['url'], $data['title'], $data['trackback_url']);
  121. $track = Services_Trackback::create($data);
  122. $result = trim($track->getAutodiscoveryCode());
  123. $this->assertSame($xml, $result);
  124. }
  125. function testReceive()
  126. {
  127. global $trackbackData;
  128. $postData = $trackbackData['nospam'];
  129. $data = $postData;
  130. $data['id'] = 1;
  131. // Not set during receive()
  132. // unset($data['host']);
  133. unset($data['trackback_url']);
  134. $recTrack = Services_Trackback::create(array('id' => 1));
  135. $recTrack->receive($postData);
  136. $fakeTrack = Services_Trackback::create($data);
  137. $fakeTrack->set('extra', $_SERVER);
  138. $this->assertTrue($recTrack == $fakeTrack);
  139. }
  140. function testGetResponseSuccess()
  141. {
  142. $xml = $this->xml['testGetResponseSuccess'];
  143. $this->assertTrue(!empty($xml), "Test was unable to locate sample data");
  144. $generated_response = Services_Trackback::getResponseSuccess();
  145. $this->assertSame($xml, $generated_response);
  146. }
  147. function testGetResponseError()
  148. {
  149. $xml = $this->xml['testGetResponseError'];
  150. $this->assertTrue(!empty($xml), "Test was unable to locate sample data");
  151. $generated_error = Services_Trackback::getResponseError('Me & you', -2);
  152. $this->assertSame($xml, $generated_error);
  153. }
  154. function testAddSpamCheckSuccess()
  155. {
  156. $trackback = new Services_Trackback();
  157. $spamCheck = new Services_Trackback_SpamCheck_Mock();
  158. $result = $trackback->addSpamCheck($spamCheck);
  159. $this->assertFalse(PEAR::isError($result));
  160. }
  161. function testAddSpamCheckFailure()
  162. {
  163. $trackback = new Services_Trackback();
  164. $spamCheck = new Services_Trackback();
  165. $this->assertTrue(PEAR::isError($trackback->addSpamCheck($spamCheck)));
  166. }
  167. function testCreateSpamCheckSuccess()
  168. {
  169. global $trackbackData;
  170. $trackback = new Services_Trackback($trackbackData['nospam']);
  171. $spamCheck = Services_Trackback_SpamCheck::create('DNSBL');
  172. $this->assertTrue($trackback->createSpamCheck('DNSBL') == $spamCheck);
  173. }
  174. function testCreateSpamCheckFailure()
  175. {
  176. global $trackbackData;
  177. $trackback = new Services_Trackback($trackbackData['nospam']);
  178. $spamCheck = Services_Trackback_SpamCheck::create('DNS');
  179. $this->assertTrue(PEAR::isError($spamCheck));
  180. }
  181. function testRemoveSpamCheckSuccess()
  182. {
  183. $trackback = new Services_Trackback();
  184. $spamCheck = new Services_Trackback_SpamCheck_Mock();
  185. $trackback->addSpamCheck($spamCheck);
  186. $this->assertFalse(PEAR::isError($result), "Failed to add SpamCheck, can't complete test");
  187. $result = $trackback->removeSpamCheck($spamCheck);
  188. $this->assertFalse(PEAR::isError($result));
  189. }
  190. function testRemoveSpamCheckFailure()
  191. {
  192. $trackback = new Services_Trackback();
  193. $spamCheck = new Services_Trackback_SpamCheck();
  194. $trackback->addSpamCheck($spamCheck);
  195. $spamCheck2 = new Services_Trackback_SpamCheck();
  196. $this->assertTrue(PEAR::isError($trackback->removeSpamCheck($spamCheck2)));
  197. }
  198. function testFromArray()
  199. {
  200. global $trackbackData;
  201. $fakeTrack = new Services_Trackback;
  202. $fakeTrack->_data = $trackbackData['nospam'];
  203. $realTrack = new Services_Trackback;
  204. $realTrack->_fromArray($trackbackData['nospam']);
  205. $this->assertTrue($realTrack == $fakeTrack);
  206. }
  207. function testGetContent()
  208. {
  209. $this->markTestSkipped("See Bug #13456");
  210. global $trackbackData;
  211. $trackback = Services_Trackback::create($trackbackData['nospam']);
  212. $url = 'http://schlitt.info/projects/PEAR/Services_Trackback/test_getContent.txt';
  213. $fakeRes = "Test text.\n";
  214. $res = $trackback->_getContent($url);
  215. if (PEAR::isError($res)) {
  216. $this->fail($res->getMessage());
  217. return;
  218. }
  219. $this->assertTrue(trim($res) == trim($fakeRes));
  220. }
  221. function testGetEncodedData()
  222. {
  223. $in = array(
  224. 'foo' => 'bar & baz',
  225. 'bar' => 'foo << baz',
  226. 'baz' => 'foo && bar'
  227. );
  228. $out = array(
  229. 'foo' => 'bar &amp; baz',
  230. 'bar' => 'foo &lt;&lt; baz',
  231. 'baz' => 'foo &amp;&amp; bar'
  232. );
  233. $this->assertTrue(Services_Trackback::_getEncodedData(array('foo', 'bar', 'baz'), $in) == $out);
  234. }
  235. function testGetDecodedData()
  236. {
  237. $in = array(
  238. 'foo' => 'bar & baz',
  239. 'bar' => 'foo << baz',
  240. 'baz' => 'foo && bar'
  241. );
  242. $out = array(
  243. 'foo' => 'bar & baz',
  244. 'baz' => 'foo && bar'
  245. );
  246. $this->assertTrue(Services_Trackback::_getDecodedData(array('foo', 'baz'), $in) == $out);
  247. }
  248. function testCheckDataTrue()
  249. {
  250. $keys = array('id', 'test');
  251. $data = array('id' => 1, 'test' => 'x', 'test2' => 0);
  252. $this->assertTrue(Services_Trackback::_checkData($keys, $data));
  253. }
  254. function testCheckDataFalse()
  255. {
  256. $keys = array('id', 'test');
  257. $data = array('id' => 1, 'test2' => 0);
  258. $this->assertTrue(PEAR::isError(Services_Trackback::_checkData($keys, $data)));
  259. }
  260. function testCheckURLsTrue1()
  261. {
  262. $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW;
  263. $url1 = "http://www.example.com/trackback/index.php";
  264. $url2 = "http://www.example.net/trackbike/index.htm";
  265. $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness));
  266. }
  267. function testCheckURLsTrue2()
  268. {
  269. $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  270. $url1 = "http://www.example.com/trackback/index.php";
  271. $url2 = "http://www.example.com/trackbike/index.htm";
  272. $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness));
  273. }
  274. function testCheckURLsTrue3()
  275. {
  276. $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH;
  277. $url1 = "http://www.example.com/trackback/index.php";
  278. $url2 = "http://www.example.com/trackback/index.php";
  279. $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness));
  280. }
  281. function testCheckURLsFalse1()
  282. {
  283. // No real test, should always return true
  284. $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW;
  285. $url1 = "http://www.example.com/trackback/index.php";
  286. $url2 = "https://test.net/trackbike/index.htm";
  287. $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness));
  288. }
  289. function testCheckURLsFalse2()
  290. {
  291. $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  292. $url1 = "http://www.example.com/trackback/index.php";
  293. $url2 = "http://www.example.net/trackback/index.php";
  294. $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness)));
  295. }
  296. function testCheckURLsFalse3()
  297. {
  298. $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH;
  299. $url1 = "http://www.example.com/trackback/index.php";
  300. $url2 = "http://www.example.com/trackback/index.htm";
  301. $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness)));
  302. }
  303. function testCheckURLsInvalid1()
  304. {
  305. // No real test, should always return true
  306. $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW;
  307. $url1 = "http://www.example.com/trackback/index.php";
  308. $url2 = "https://test.net/trackbike/index.htm";
  309. $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness));
  310. }
  311. function testCheckURLsInvalid2()
  312. {
  313. $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  314. $url1 = "http:///trackback/index.php";
  315. $url2 = "http://www.example.net/trackback/index.php";
  316. $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness)));
  317. }
  318. function testCheckURLsInvalid3()
  319. {
  320. // No real test, URLs are not invalid, but unequal
  321. $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH;
  322. $url1 = "http://www.example.com/trackback/index.php";
  323. $url2 = "http://www.example.com/trackback/index.htm";
  324. $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness)));
  325. }
  326. function testInterpretTrackbackResponseSuccess()
  327. {
  328. $xml = $this->xml['testInterpretTrackbackResponseSuccess'];
  329. $res = Services_Trackback::_interpretTrackbackResponse($xml);
  330. $this->assertTrue($res);
  331. }
  332. function testInterpretTrackbackResponseFailure()
  333. {
  334. $xml = $this->xml['testInterpretTrackbackResponseFailure'];
  335. $res = Services_Trackback::_interpretTrackbackResponse($xml);
  336. $this->assertTrue(PEAR::isError($res));
  337. }
  338. function testInterpretTrackbackResponseInvalid1()
  339. {
  340. $xml = $this->xml['testInterpretTrackbackResponseInvalid1'];
  341. $res = Services_Trackback::_interpretTrackbackResponse($xml);
  342. $this->assertTrue(PEAR::isError($res));
  343. }
  344. function testInterpretTrackbackResponseInvalid2()
  345. {
  346. $xml = $this->xml['testInterpretTrackbackResponseInvalid2'];
  347. $res = Services_Trackback::_interpretTrackbackResponse($xml);
  348. $this->assertTrue(PEAR::isError($res));
  349. }
  350. }