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.

162 lines
6.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 $http;
  6. private $api;
  7. public function setUp() {
  8. $this->http = new Telegraph\HTTPTest(dirname(__FILE__).'/data/');
  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('accepted', $status->status);
  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('error', $status->status);
  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. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  89. $this->assertNotNull($webmention->webmention_endpoint);
  90. $this->assertNull($webmention->pingback_endpoint);
  91. $this->assertEquals('accepted', $status->status);
  92. }
  93. public function testWebmentionSucceeds() {
  94. $this->_createExampleAccount();
  95. list($webmention, $callback) = $this->webmention([
  96. 'token' => 'a',
  97. 'source' => 'http://source.example.com/webmention-success',
  98. 'target' => 'http://target.example.com/webmention-success'
  99. ]);
  100. $status = $this->webmentionStatus($webmention->id);
  101. $this->assertEquals('accepted', $status->status);
  102. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  103. $this->assertEquals('http://webmention.example.com/success', $webmention->webmention_endpoint);
  104. }
  105. public function testSavesWebmentionStatusURL() {
  106. $this->_createExampleAccount();
  107. list($webmention, $callback) = $this->webmention([
  108. 'token' => 'a',
  109. 'source' => 'http://source.example.com/webmention-status-url',
  110. 'target' => 'http://target.example.com/webmention-status-url'
  111. ]);
  112. $status = $this->webmentionStatus($webmention->id);
  113. $this->assertEquals('accepted', $status->status);
  114. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  115. $this->assertEquals('http://webmention.example.com/success-with-status', $webmention->webmention_endpoint);
  116. // Make sure the status URL returned is an absolute URL
  117. $this->assertEquals('http://webmention.example.com/webmention/1000', $webmention->webmention_status_url);
  118. }
  119. public function testWebmentionFailed() {
  120. $this->_createExampleAccount();
  121. list($webmention, $callback) = $this->webmention([
  122. 'token' => 'a',
  123. 'source' => 'http://source.example.com/webmention-failed',
  124. 'target' => 'http://target.example.com/webmention-failed'
  125. ]);
  126. $status = $this->webmentionStatus($webmention->id);
  127. $this->assertEquals('error', $status->status);
  128. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  129. $this->assertEquals('http://webmention.example.com/error', $webmention->webmention_endpoint);
  130. }
  131. public function testWebmentionStatusCallback() {
  132. $this->_createExampleAccount();
  133. list($webmention, $callback) = $this->webmention([
  134. 'token' => 'a',
  135. 'source' => 'http://source.example.com/webmention-success',
  136. 'target' => 'http://target.example.com/webmention-success',
  137. 'callback' => 'http://source.example.com/callback'
  138. ]);
  139. $status = $this->webmentionStatus($webmention->id);
  140. $this->assertEquals('accepted', $status->status);
  141. $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
  142. $this->assertEquals('http://webmention.example.com/success', $webmention->webmention_endpoint);
  143. $this->assertEquals('Callback was successful', trim($callback['body']));
  144. }
  145. }