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.

103 lines
3.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 $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. }
  66. public function testPingbackFailed() {
  67. $this->_createExampleAccount();
  68. $webmention = $this->webmention([
  69. 'token' => 'a',
  70. 'source' => 'http://source.example.com/pingback-failed',
  71. 'target' => 'http://target.example.com/pingback-failed'
  72. ]);
  73. $status = $this->webmentionStatus($webmention->id);
  74. $this->assertEquals($status->status, 'pingback_error');
  75. }
  76. public function testWebmentionTakesPriorityOverPingback() {
  77. $this->_createExampleAccount();
  78. $webmention = $this->webmention([
  79. 'token' => 'a',
  80. 'source' => 'http://source.example.com/webmention-and-pingback',
  81. 'target' => 'http://target.example.com/webmention-success'
  82. ]);
  83. $status = $this->webmentionStatus($webmention->id);
  84. $this->assertEquals($status->status, 'webmention_accepted');
  85. }
  86. public function testWebmentionFailed() {
  87. }
  88. }