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.

159 lines
6.5 KiB

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