Services_Trackback
[ class tree: Services_Trackback ] [ index: Services_Trackback ] [ all elements ]

Source for file Services_Trackback_Test.php

Documentation is available at Services_Trackback_Test.php

  1. <?php
  2.  
  3. // Includepath for local CVS development
  4. // set_include_path('/cvs/pear/Services_Trackback'.PATH_SEPARATOR.get_include_path());
  5.  
  6.     // {{{ require_once
  7.  
  8. // Services_Trackback classes
  9. require_once 'Services/Trackback.php';
  10. require_once 'Services/Trackback/SpamCheck.php';
  11.  
  12. // Unittest suite
  13. require_once 'PHPUnit.php';
  14.  
  15. // Testdata
  16. require_once dirname(__FILE__).'/trackback_data.php';
  17.  
  18.     // }}}
  19.  
  20. class Webservices_Trackback_TestCase extends PHPUnit_TestCase
  21. {
  22.     
  23.     // {{{ Webservices_Trackback_TestCase()
  24.     
  25.     // constructor of the test suite
  26.     function Webservices_Trackback_TestCase($name{
  27.        $this->PHPUnit_TestCase($name);
  28.     }
  29.  
  30.     // }}}
  31.     // {{{ setup()
  32.     
  33.     function setUp({
  34.     }
  35.  
  36.     // }}}
  37.     // {{{ tearDown()
  38.     
  39.     function tearDown({
  40.     }
  41.  
  42.     // }}}
  43.     // {{{ Test create()
  44.  
  45.     function test_create({
  46.         global $trackbackData;
  47.         $options = array(
  48.             'strictness'        => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  49.             'timeout'           => 10,
  50.             'fetchlines'        => 100,
  51.             'httpRequest'       => array(
  52.                 'allowRedirects'    => false,
  53.                 'maxRedirects'      => 0,
  54.                 'useragent'         => 'Mozilla 10.0',
  55.             ),
  56.         );
  57.         $fakeTrack = new Services_Trackback;
  58.         $fakeTrack->_options = $options;
  59.         $fakeTrack->_data = $trackbackData['nospam'];
  60.         $this->assertTrue(Services_Trackback::create($trackbackData['nospam']$options== $fakeTrack);
  61.     }
  62.  
  63.     // }}}
  64.     // {{{ Test setOptions()
  65.  
  66.     function test_setOptions_success({
  67.         $options = array(
  68.             'strictness'        => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  69.             'timeout'           => 10,
  70.             'fetchlines'        => 100,
  71.             'httpRequest'       => array(
  72.                 'allowRedirects'    => false,
  73.                 'maxRedirects'      => 0,
  74.                 'useragent'         => 'Mozilla 10.0',
  75.             ),
  76.         );
  77.         $fakeTrack = new Services_Trackback;
  78.         $fakeTrack->_options = $options;
  79.         $realTrack = new Services_Trackback;
  80.         $realTrack->setOptions($options);
  81.         $this->assertTrue($realTrack == $fakeTrack);
  82.     }
  83.  
  84.     // }}}
  85.     // {{{ Test getOptions()
  86.  
  87.     function test_getOptions_success({
  88.         $options = array(
  89.             'strictness'        => SERVICES_TRACKBACK_STRICTNESS_HIGH,
  90.             'timeout'           => 10,
  91.             'fetchlines'        => 100,
  92.             'httpRequest'       => array(
  93.                 'allowRedirects'    => false,
  94.                 'maxRedirects'      => 0,
  95.                 'useragent'         => 'Mozilla 10.0',
  96.             ),
  97.         );
  98.         $track = new Services_Trackback;
  99.         $track->_options = $options;
  100.         $this->assertTrue($track->getOptions(== $options);
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ Test autodiscover()
  105.    
  106.    function test_autodiscover_success()
  107.     {
  108.         $data = array(
  109.             'id' => 'Test',
  110.             'url' => 'http://pear.php.net/package/net_ftp'
  111.         );
  112.         $track1 Services_Trackback::create($data);
  113.         $track1->autodiscover();
  114.  
  115.         $data['trackback_url''http://pear.php.net/trackback/trackback.php?id=Net_FTP';
  116.         $track2 Services_Trackback::create($data);
  117.         $this->assertTrue($track1 == $track2);
  118.     }
  119.     function test_autodiscover_failure()
  120.     {
  121.         $data = array(
  122.             'id' => 'Test',
  123.             'url' => 'http://pear.php.net/'
  124.         );
  125.         $track1 Services_Trackback::create($data);
  126.         $res $track1->autodiscover();
  127.         $this->assertTrue(PEAR::isError($res));
  128.     }
  129.     
  130.     // }}}
  131.     // {{{Test send()
  132.  
  133.     function test_send()
  134.     {
  135.         global $trackbackData;
  136.         $track Services_Trackback::create($trackbackData['nospam']);
  137.     }
  138.  
  139.     // }}}
  140.     // {{{Test getAutodiscoveryCode()
  141.  
  142.     function test_getAutodiscoveryCode_nocomments()
  143.     {
  144.         global $trackbackData;
  145.         $data $trackbackData['nospam'];
  146.         
  147.         $xml = <<<EOD
  148. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  149.     xmlns:dc="http://purl.org/dc/elements/1.1/"
  150.     xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  151.     <rdf:Description
  152.         rdf:about="%s"
  153.         dc:identifier="%s"
  154.         dc:title="%s"
  155.         trackback:ping="%s" />
  156. </rdf:RDF>
  157.  
  158. EOD;
  159.         $xml sprintf($xml$data['url']$data['url']$data['title']$data['trackback_url']);
  160.         $track Services_Trackback::create($data);
  161.         $this->assertTrue($track->getAutodiscoveryCode(false== $xml);
  162.     }
  163.     function test_getAutodiscoveryCode_comments()
  164.     {
  165.         global $trackbackData;
  166.         $data $trackbackData['nospam'];
  167.         
  168.         $xml = <<<EOD
  169. <!--
  170. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  171.     xmlns:dc="http://purl.org/dc/elements/1.1/"
  172.     xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  173.     <rdf:Description
  174.         rdf:about="%s"
  175.         dc:identifier="%s"
  176.         dc:title="%s"
  177.         trackback:ping="%s" />
  178. </rdf:RDF>
  179. -->
  180.  
  181. EOD;
  182.         $xml sprintf($xml$data['url']$data['url']$data['title']$data['trackback_url']);
  183.         $track Services_Trackback::create($data);
  184.         $this->assertTrue($track->getAutodiscoveryCode(== $xml);
  185.     }
  186.  
  187.     // }}}
  188.     // {{{ Test receive()
  189.     
  190.     function test_receive()
  191.     {
  192.         global $trackbackData;
  193.         $postData $trackbackData['nospam'];
  194.         $data $postData;
  195.         
  196.         $data['id'= 1;
  197.         // Not set during receive()
  198.         // unset($data['host']);
  199.         unset($data['trackback_url']);
  200.         
  201.         $recTrack Services_Trackback::create(array('id' => 1));
  202.         $recTrack->receive($postData);
  203.         $this->assertTrue($recTrack == Services_Trackback::create($data));
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ Test getResponseSuccess()
  208.     
  209.     function test_getResponseSuccess()
  210.     {
  211.         $xml = <<<EOD
  212. <?xml version="1.0" encoding="iso-8859-1"?>
  213. <response>
  214. <error>0</error>
  215. </response>
  216. EOD;
  217.         $this->assertTrue(Services_Trackback::getResponseSuccess(== $xml);
  218.     }
  219.  
  220.     // }}}
  221.     // {{{ Test getResponseError()
  222.     
  223.     function test_getResponseError()
  224.     {
  225.         $xml = <<<EOD
  226. <?xml version="1.0" encoding="iso-8859-1"?>
  227. <response>
  228. <error>-2</error>
  229. <message>Me &amp; you</message>
  230. </response>
  231. EOD;
  232.         $this->assertTrue(Services_Trackback::getResponseError('Me & you'-2== $xml);
  233.     }
  234.     
  235.     // }}}
  236.     // {{{ Test addSpamCheck
  237.     
  238.     function test_addSpamCheck_success()
  239.     {
  240.         $trackback = new Services_Trackback();
  241.         $spamCheck = new Services_Trackback_SpamCheck();
  242.         $this->assertTrue($trackback->addSpamCheck($spamCheck));
  243.     }
  244.     
  245.     function test_addSpamCheck_failure()
  246.     {
  247.         $trackback = new Services_Trackback();
  248.         $spamCheck = new Services_Trackback();
  249.         $this->assertTrue(PEAR::isError($trackback->addSpamCheck($spamCheck)));
  250.     }
  251.     
  252.     // }}}
  253.     // {{{ Test createSpamCheck
  254.     
  255.     function test_createSpamCheck_success()
  256.     {
  257.         global $trackbackData;
  258.         $trackback = new Services_Trackback($trackbackData['nospam']);
  259.         $spamCheck Services_Trackback_SpamCheck::create('DNSBL');
  260.         $this->assertTrue($trackback->createSpamCheck('DNSBL'== $spamCheck);
  261.     }
  262.     
  263.     function test_createSpamCheck_failure()
  264.     {
  265.         global $trackbackData;
  266.         $trackback = new Services_Trackback($trackbackData['nospam']);
  267.         $spamCheck Services_Trackback_SpamCheck::create('DNS');
  268.         $this->assertTrue(PEAR::isError($spamCheck));
  269.     }
  270.     
  271.     // }}}
  272.     // {{{ Test removeSpamCheck
  273.     
  274.     function test_removeSpamCheck_success()
  275.     {
  276.         $trackback = new Services_Trackback();
  277.         $spamCheck = new Services_Trackback_SpamCheck();
  278.         $trackback->addSpamCheck($spamCheck);
  279.         $this->assertTrue($trackback->removeSpamCheck($spamCheck));
  280.     }
  281.     
  282.     function test_removeSpamCheck_failure()
  283.     {
  284.         $trackback = new Services_Trackback();
  285.         $spamCheck = new Services_Trackback_SpamCheck();
  286.         $trackback->addSpamCheck($spamCheck);
  287.         $spamCheck2 = new Services_Trackback_SpamCheck();
  288.         $this->assertTrue(PEAR::isError($trackback->removeSpamCheck($spamCheck2)));
  289.     }
  290.     
  291.     // }}}
  292.     // {{{ Test _fromArray()
  293.  
  294.     function test_fromArray({
  295.         global $trackbackData;
  296.         $fakeTrack = new Services_Trackback;
  297.         $fakeTrack->_data = $trackbackData['nospam'];
  298.         $realTrack = new Services_Trackback;
  299.         $realTrack->_fromArray($trackbackData['nospam']);
  300.         $this->assertTrue($realTrack == $fakeTrack);
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ Test _getContent()
  305.  
  306.     function test_getContent({
  307.         global $trackbackData;
  308.         $trackback Services_Trackback::create($trackbackData['nospam']);
  309.         $url 'http://www.example.com';
  310.         $fakeRes = <<<EOD
  311. <HTML>
  312. <HEAD>
  313.   <TITLE>Example Web Page</TITLE>
  314. </HEAD> 
  315. <body>  
  316. <p>You have reached this web page by typing &quot;example.com&quot;,
  317. &quot;example.net&quot;,
  318.   or &quot;example.org&quot; into your web browser.</p>
  319. <p>These domain names are reserved for use in documentation and are not available 
  320.   for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 
  321.   2606</a>, Section 3.</p>
  322. </BODY>
  323. </HTML>
  324. EOD;
  325.    
  326.         $res $trackback->_getContent($url);
  327.         if (PEAR::isError($res)) {
  328.             $this->fail($res->getMessage());
  329.             return;
  330.         }
  331.  
  332.         $this->assertTrue(trim($res== trim($fakeRes));
  333.     }
  334.  
  335.     // }}}
  336.     // {{{ Test _getEncodedData()
  337.  
  338.     function test_getEncodedData({
  339.         $in = array(
  340.             'foo' => 'bar & baz',
  341.             'bar' => 'foo << baz',
  342.             'baz' => 'foo && bar'
  343.         );
  344.  
  345.         $out = array(
  346.             'foo' => 'bar &amp; baz',
  347.             'bar' => 'foo &lt;&lt; baz',
  348.             'baz' => 'foo &amp;&amp; bar'
  349.         );
  350.         
  351.         $this->assertTrue(Services_Trackback::_getEncodedData(array('foo''bar''baz')$in== $out);
  352.     }
  353.     // }}}
  354.     // {{{ Test _getDecodedData()
  355.  
  356.     function test_getDecodedData({
  357.         $in = array(
  358.             'foo' => 'bar & baz',
  359.             'bar' => 'foo << baz',
  360.             'baz' => 'foo && bar'
  361.         );
  362.  
  363.         $out = array(
  364.             'foo' => 'bar & baz',
  365.             'baz' => 'foo && bar'
  366.         );
  367.         
  368.         $this->assertTrue(Services_Trackback::_getDecodedData(array('foo''baz')$in== $out);
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ Test _checkData
  373.     
  374.     function test_checkData_true()
  375.     {
  376.         $keys = array('id''test');
  377.         $data = array('id' => 1'test' => 'x''test2' => 0);
  378.         $this->assertTrue(Services_Trackback::_checkData($keys$data));
  379.     }
  380.     function test_checkData_false()
  381.     {
  382.         $keys = array('id''test');
  383.         $data = array('id' => 1'test2' => 0);
  384.         $this->assertTrue(PEAR::isError(Services_Trackback::_checkData($keys$data)));
  385.     }
  386.  
  387.     // }}}
  388.     // {{{ Test _checkURLs()
  389.     
  390.     function test_checkURLs_true_1()
  391.     {
  392.         $strictness SERVICES_TRACKBACK_STRICTNESS_LOW;
  393.         $url1 "http://www.example.com/trackback/index.php";
  394.         $url2 "http://www.example.net/trackbike/index.htm";
  395.         $this->assertTrue(Services_Trackback::_checkURLs($url1$url2$strictness));
  396.     }
  397.     function test_checkURLs_true_2()
  398.     {
  399.         $strictness SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  400.         $url1 "http://www.example.com/trackback/index.php";
  401.         $url2 "http://www.example.com/trackbike/index.htm";
  402.         $this->assertTrue(Services_Trackback::_checkURLs($url1$url2$strictness));
  403.     }
  404.     function test_checkURLs_true_3()
  405.     {
  406.         $strictness SERVICES_TRACKBACK_STRICTNESS_HIGH;
  407.         $url1 "http://www.example.com/trackback/index.php";
  408.         $url2 "http://www.example.com/trackback/index.php";
  409.         $this->assertTrue(Services_Trackback::_checkURLs($url1$url2$strictness));
  410.     }
  411.  
  412.  
  413.     function test_checkURLs_false_1()
  414.     {
  415.         // No real test, should always return true
  416.         $strictness SERVICES_TRACKBACK_STRICTNESS_LOW;
  417.         $url1 "http://www.example.com/trackback/index.php";
  418.         $url2 "https://test.net/trackbike/index.htm";
  419.         $this->assertTrue(Services_Trackback::_checkURLs($url1$url2$strictness));
  420.     }
  421.     function test_checkURLs_false_2()
  422.     {
  423.         $strictness SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  424.         $url1 "http://www.example.com/trackback/index.php";
  425.         $url2 "http://www.example.net/trackback/index.php";
  426.         $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1$url2$strictness)));
  427.     }
  428.     function test_checkURLs_false_3()
  429.     {
  430.         $strictness SERVICES_TRACKBACK_STRICTNESS_HIGH;
  431.         $url1 "http://www.example.com/trackback/index.php";
  432.         $url2 "http://www.example.com/trackback/index.htm";
  433.         $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1$url2$strictness)));
  434.     }
  435.  
  436.     function test_checkURLs_invalid_1()
  437.     {
  438.         // No real test, should always return true
  439.         $strictness SERVICES_TRACKBACK_STRICTNESS_LOW;
  440.         $url1 "http://www.example.com/trackback/index.php";
  441.         $url2 "https://test.net/trackbike/index.htm";
  442.         $this->assertTrue(Services_Trackback::_checkURLs($url1$url2$strictness));
  443.     }
  444.     function test_checkURLs_invalid_2()
  445.     {
  446.         $strictness SERVICES_TRACKBACK_STRICTNESS_MIDDLE;
  447.         $url1 "http:///trackback/index.php";
  448.         $url2 "http://www.example.net/trackback/index.php";
  449.         $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1$url2$strictness)));
  450.     }
  451.     function test_checkURLs_invalid_3()
  452.     {
  453.         // No real test, URLs are not invalid, but unequal
  454.         $strictness SERVICES_TRACKBACK_STRICTNESS_HIGH;
  455.         $url1 "http://www.example.com/trackback/index.php";
  456.         $url2 "http://www.example.com/trackback/index.htm";
  457.         $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1$url2$strictness)));
  458.     }
  459.  
  460.     // 
  461.  
  462.     // }}}
  463.     // {{{ Test _interpretTrackbackResponse()
  464.  
  465.     function test_interpretTrackbackResponse_success({
  466.         $xml = <<<EOD
  467. <?xml version='1.0' encoding='iso-8859-1'?>
  468. <response>
  469. <error>0</error>
  470. </response>
  471. EOD;
  472.         $res Services_Trackback::_interpretTrackbackResponse($xml);
  473.         $this->assertTrue($res);
  474.     }
  475.     
  476.     function test_interpretTrackbackResponse_failure({
  477.         $xml = <<<EOD
  478. <?xml version='1.0' encoding='iso-8859-1'?>
  479. <response>
  480. <error>-1</error>
  481. <message>No more trackbacks from this host</message>
  482. </response>
  483. EOD;
  484.         $res Services_Trackback::_interpretTrackbackResponse($xml);
  485.         $this->assertTrue(PEAR::isError($res));
  486.     }
  487.     
  488.     function test_interpretTrackbackResponse_invalid_1({
  489.         $xml = <<<EOD
  490. <?xml version='1.0' encoding='iso-8859-1'?>
  491. <response>
  492. <error></error>
  493. <message>No more trackbacks from this host</message>
  494. </response>
  495. EOD;
  496.         $res Services_Trackback::_interpretTrackbackResponse($xml);
  497.         $this->assertTrue(PEAR::isError($res));
  498.     }
  499.     
  500.     function test_interpretTrackbackResponse_invalid_2({
  501.         $xml = <<<EOD
  502. <?xml version='1.0' encoding='iso-8859-1'?>
  503. <response>
  504. </response>
  505. EOD;
  506.  
  507.         $res Services_Trackback::_interpretTrackbackResponse($xml);
  508.         $this->assertTrue(PEAR::isError($res));
  509.     }
  510.  
  511.     // }}}
  512.  
  513. }
  514.  
  515. $suite  = new PHPUnit_TestSuite("Webservices_Trackback_TestCase");
  516. $result = PHPUnit::run($suite);
  517.  
  518. echo $result->toString();
  519.  
  520. ?>

Documentation generated on Mon, 11 Mar 2019 14:36:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.