mollom.class.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for Mollom class.
  5. *
  6. * @todo After final Mollom class commit, move all low-level tests from
  7. * mollom.test into this file.
  8. */
  9. /**
  10. * Tests Mollom class functionality.
  11. */
  12. class MollomClassUnitTestCase extends DrupalUnitTestCase {
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Mollom class',
  16. 'description' => 'Tests Mollom class functionality.',
  17. 'group' => 'Mollom',
  18. );
  19. }
  20. function setUp() {
  21. parent::setUp();
  22. // DrupalUnitTestCase does not autoload classes for whatever reason.
  23. module_load_include('inc', 'mollom', 'includes/mollom.class');
  24. }
  25. /**
  26. * Asserts that two values belonging to the same variable are equal.
  27. *
  28. * Checks to see whether two values, which belong to the same variable name or
  29. * identifier, are equal and logs a readable assertion message.
  30. *
  31. * @param $name
  32. * A name or identifier to use in the assertion message.
  33. * @param $first
  34. * The first value to check.
  35. * @param $second
  36. * The second value to check.
  37. *
  38. * @return
  39. * TRUE if the assertion succeeded, FALSE otherwise.
  40. *
  41. * @see MollomWebTestCase::assertNotSame()
  42. *
  43. * @todo D8: Move into core. This improved assertEqual() did not get into D7,
  44. * since the function signature differs and it's plenty of work to manually
  45. * update all assertEqual() invocations throughout all tests.
  46. */
  47. protected function assertSame($name, $first, $second) {
  48. $message = t("@name: @first is equal to @second.", array(
  49. '@name' => $name,
  50. '@first' => var_export($first, TRUE),
  51. '@second' => var_export($second, TRUE),
  52. ));
  53. $this->assertEqual($first, $second, $message);
  54. }
  55. /**
  56. * Asserts that two values belonging to the same variable are not equal.
  57. *
  58. * Checks to see whether two values, which belong to the same variable name or
  59. * identifier, are not equal and logs a readable assertion message.
  60. *
  61. * @param $name
  62. * A name or identifier to use in the assertion message.
  63. * @param $first
  64. * The first value to check.
  65. * @param $second
  66. * The second value to check.
  67. *
  68. * @return
  69. * TRUE if the assertion succeeded, FALSE otherwise.
  70. *
  71. * @see MollomWebTestCase::assertSame()
  72. */
  73. protected function assertNotSame($name, $first, $second) {
  74. $message = t("@name: @first is not equal to @second.", array(
  75. '@name' => $name,
  76. '@first' => var_export($first, TRUE),
  77. '@second' => var_export($second, TRUE),
  78. ));
  79. $this->assertNotEqual($first, $second, $message);
  80. }
  81. /**
  82. * Tests Mollom::httpBuildQuery().
  83. */
  84. function testHttpBuildQuery() {
  85. // Single parameter.
  86. $input = array('checks' => 'spam');
  87. $expected = 'checks=spam';
  88. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  89. // Multiple parameters, numbers.
  90. $input = array('foo' => 1, 'bar' => 2);
  91. $expected = 'bar=2&foo=1';
  92. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  93. // Parameter with multiple values.
  94. $input = array('checks' => array('spam', 'profanity'));
  95. $expected = 'checks=profanity&checks=spam';
  96. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  97. // Parameter with multiple values, empty.
  98. $input = array('checks' => array('spam', ''));
  99. $expected = 'checks=&checks=spam';
  100. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  101. // Parameter with multiple values, NULL.
  102. $input = array('checks' => array('spam', NULL));
  103. $expected = 'checks=&checks=spam';
  104. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  105. // Multiple parameters with NULL value.
  106. $input = array('foo' => 1, 'checks' => NULL);
  107. $expected = 'checks=&foo=1';
  108. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  109. // Multiple parameters with multiple values.
  110. // (official OAuth protocol example)
  111. // @see RFC 5849 3.4.1.3.1
  112. $input = array(
  113. 'b5' => '=%3D',
  114. 'a3' => array('a', '2 q'),
  115. 'c@' => '',
  116. 'a2' => 'r b',
  117. 'oauth_consumer_key' => '9djdj82h48djs9d2',
  118. 'oauth_token' => 'kkk9d7dh3k39sjv7',
  119. 'oauth_signature_method' => 'HMAC-SHA1',
  120. 'oauth_timestamp' => '137131201',
  121. 'oauth_nonce' => '7d8f3e4a',
  122. 'c2' => '',
  123. );
  124. $expected = 'a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7';
  125. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  126. // Parameter with recursive multiple values.
  127. $input = array('checks' => array(array('spam'), array('profanity')));
  128. $expected = 'checks=profanity&checks=spam';
  129. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  130. // Parameter with multiple named values.
  131. // @todo Drop support for this?
  132. $input = array('checks' => array('foo' => 'spam', 'bar' => 'profanity'));
  133. $expected = 'checks[bar]=profanity&checks[foo]=spam';
  134. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  135. // Prior to PHP 5.3.0, rawurlencode() encoded tildes (~) as per RFC 1738.
  136. $input = array(
  137. 'reserved' => ':/?#[]@!$&\'()*+,;=',
  138. 'unreserved' => '-._~',
  139. );
  140. $expected = 'reserved=%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D&unreserved=-._~';
  141. $this->assertSame(var_export($input, TRUE), Mollom::httpBuildQuery($input), $expected);
  142. }
  143. /**
  144. * Tests Mollom::httpParseQuery().
  145. */
  146. function testHttpParseQuery() {
  147. $input = 'foo=1&bar=2';
  148. $expected = array('foo' => 1, 'bar' => 2);
  149. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  150. $input = 'checks=spam&checks=profanity';
  151. $expected = array('checks' => array('spam', 'profanity'));
  152. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  153. // Mollom::httpParseQuery() does not attempt to work transparently. Thus,
  154. // multiple parameter names containing brackets itself (regular PHP syntax)
  155. // will lead to an "unexpected" result. Although it wouldn't be hard to add
  156. // support for this, there's currently no need for it.
  157. $input = 'checks[]=spam&checks[]=profanity';
  158. $expected = array('checks' => array(array('spam'), array('profanity')));
  159. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  160. $input = 'checks=spam&checks=';
  161. $expected = array('checks' => array('spam', ''));
  162. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  163. $input = 'checks=spam&checks';
  164. $expected = array('checks' => array('spam', ''));
  165. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  166. $input = 'checks=spam&';
  167. $expected = array('checks' => 'spam');
  168. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  169. $input = 'checks=spam';
  170. $expected = array('checks' => 'spam');
  171. $this->assertSame($input, Mollom::httpParseQuery($input), $expected);
  172. }
  173. /**
  174. * Tests Mollom::parseXML().
  175. */
  176. function testParseXML() {
  177. $header = '<?xml version="1.0"?>';
  178. $input = $header . <<<EOF
  179. <response>
  180. <code>0</code>
  181. <message>Foo.</message>
  182. <content>
  183. <contentId>321</contentId>
  184. <languages>
  185. <language>
  186. <languageCode>en</languageCode>
  187. <languageScore>1.0</languageScore>
  188. </language>
  189. <language>
  190. <languageCode>de</languageCode>
  191. <languageScore>0.5</languageScore>
  192. </language>
  193. </languages>
  194. </content>
  195. </response>
  196. EOF;
  197. $expected = array(
  198. 'code' => 0,
  199. 'message' => 'Foo.',
  200. 'content' => array(
  201. 'contentId' => 321,
  202. 'languages' => array(
  203. array('languageCode' => 'en', 'languageScore' => 1.0),
  204. array('languageCode' => 'de', 'languageScore' => 0.5),
  205. ),
  206. ),
  207. );
  208. $this->assertSame($input, Mollom::parseXML(new SimpleXmlIterator($input)), $expected);
  209. $input = $header . <<<EOF
  210. <response>
  211. <code>0</code>
  212. <message></message>
  213. <site>
  214. <publicKey>321</publicKey>
  215. <servers>
  216. <server>http://foo</server>
  217. <server>http://bar</server>
  218. </servers>
  219. </site>
  220. </response>
  221. EOF;
  222. $expected = array(
  223. 'code' => 0,
  224. 'message' => '',
  225. 'site' => array(
  226. 'publicKey' => 321,
  227. 'servers' => array(
  228. 'http://foo',
  229. 'http://bar',
  230. ),
  231. ),
  232. );
  233. $this->assertSame($input, Mollom::parseXML(new SimpleXmlIterator($input)), $expected);
  234. }
  235. /**
  236. * Test Mollom::addAuthentication().
  237. *
  238. * @todo Requires class instance for unit testing.
  239. *
  240. * - Reserved characters (such as '%') have to be double-encoded in signature base string.
  241. * - Compare signature base string with PECL OAuth result, if available?
  242. */
  243. }