You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.6 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class ProcessTest extends PHPUnit_Framework_TestCase {
  5. private $client;
  6. public function setUp() {
  7. $this->client = new API();
  8. $this->client->http = new Telegraph\HTTPTest();
  9. ORM::for_table('users')->raw_query('TRUNCATE users')->delete_many();
  10. ORM::for_table('roles')->raw_query('TRUNCATE roles')->delete_many();
  11. ORM::for_table('sites')->raw_query('TRUNCATE sites')->delete_many();
  12. ORM::for_table('webmentions')->raw_query('TRUNCATE webmentions')->delete_many();
  13. ORM::for_table('webmention_status')->raw_query('TRUNCATE webmention_status')->delete_many();
  14. }
  15. private function _createExampleAccount() {
  16. $user = ORM::for_table('users')->create();
  17. $user->url = 'http://example.com';
  18. $user->save();
  19. $site = ORM::for_table('sites')->create();
  20. $site->name = 'Example';
  21. $site->created_by = $user->id();
  22. $site->save();
  23. $role = ORM::for_table('roles')->create();
  24. $role->site_id = $site->id();
  25. $role->user_id = $user->id();
  26. $role->role = 'owner';
  27. $role->token = 'a';
  28. $role->save();
  29. }
  30. private function webmention($params) {
  31. $request = new Request($params);
  32. $response = new Response();
  33. $response = $this->client->webmention($request, $response);
  34. $webmention = ORM::for_table('webmentions')->where(['source' => $params['source'], 'target' => $params['target']])->find_one();
  35. $client = new IndieWeb\MentionClientTest();
  36. $client::$dataDir = dirname(__FILE__) . '/data/';
  37. if(!is_object($webmention)) {
  38. throw new Exception("No webmention was queued for this test");
  39. }
  40. Telegraph\Webmention::send($webmention->id, $client);
  41. return $webmention;
  42. }
  43. private static function webmentionStatus($id) {
  44. return ORM::for_table('webmention_status')->where(['webmention_id'=>$id])->order_by_desc('created_at')->find_one();
  45. }
  46. public function testNoEndpoint() {
  47. $this->_createExampleAccount();
  48. $webmention = $this->webmention([
  49. 'token' => 'a',
  50. 'source' => 'http://source.example.com/no-endpoint',
  51. 'target' => 'http://target.example.com/no-endpoint'
  52. ]);
  53. $status = $this->webmentionStatus($webmention->id);
  54. $this->assertEquals($status->status, 'not_supported');
  55. }
  56. public function testPingbackSuccess() {
  57. $this->_createExampleAccount();
  58. $webmention = $this->webmention([
  59. 'token' => 'a',
  60. 'source' => 'http://source.example.com/pingback-success',
  61. 'target' => 'http://target.example.com/pingback-success'
  62. ]);
  63. $status = $this->webmentionStatus($webmention->id);
  64. $this->assertEquals($status->status, 'pingback_accepted');
  65. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  66. $this->assertEquals('http://pingback.example.com/success', $webmention->pingback_endpoint);
  67. }
  68. public function testPingbackFailed() {
  69. $this->_createExampleAccount();
  70. $webmention = $this->webmention([
  71. 'token' => 'a',
  72. 'source' => 'http://source.example.com/pingback-failed',
  73. 'target' => 'http://target.example.com/pingback-failed'
  74. ]);
  75. $status = $this->webmentionStatus($webmention->id);
  76. $this->assertEquals($status->status, 'pingback_error');
  77. }
  78. public function testWebmentionTakesPriorityOverPingback() {
  79. $this->_createExampleAccount();
  80. $webmention = $this->webmention([
  81. 'token' => 'a',
  82. 'source' => 'http://source.example.com/webmention-and-pingback',
  83. 'target' => 'http://target.example.com/webmention-success'
  84. ]);
  85. $status = $this->webmentionStatus($webmention->id);
  86. $this->assertEquals($status->status, 'webmention_accepted');
  87. }
  88. public function testWebmentionSucceeds() {
  89. $this->_createExampleAccount();
  90. $webmention = $this->webmention([
  91. 'token' => 'a',
  92. 'source' => 'http://source.example.com/webmention-success',
  93. 'target' => 'http://target.example.com/webmention-success'
  94. ]);
  95. $status = $this->webmentionStatus($webmention->id);
  96. $this->assertEquals($status->status, 'webmention_accepted');
  97. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  98. $this->assertEquals('http://webmention.example.com/success', $webmention->webmention_endpoint);
  99. }
  100. public function testSavesWebmentionStatusURL() {
  101. $this->_createExampleAccount();
  102. $webmention = $this->webmention([
  103. 'token' => 'a',
  104. 'source' => 'http://source.example.com/webmention-status-url',
  105. 'target' => 'http://target.example.com/webmention-status-url'
  106. ]);
  107. $status = $this->webmentionStatus($webmention->id);
  108. $this->assertEquals($status->status, 'webmention_accepted');
  109. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  110. $this->assertEquals('http://webmention.example.com/success-with-status', $webmention->webmention_endpoint);
  111. // Make sure the status URL returned is an absolute URL
  112. $this->assertEquals('http://webmention.example.com/webmention/1000', $webmention->webmention_status_url);
  113. }
  114. public function testWebmentionFailed() {
  115. $this->_createExampleAccount();
  116. $webmention = $this->webmention([
  117. 'token' => 'a',
  118. 'source' => 'http://source.example.com/webmention-failed',
  119. 'target' => 'http://target.example.com/webmention-failed'
  120. ]);
  121. $status = $this->webmentionStatus($webmention->id);
  122. $this->assertEquals($status->status, 'webmention_error');
  123. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  124. $this->assertEquals('http://webmention.example.com/error', $webmention->webmention_endpoint);
  125. }
  126. }