ActivitypubProfileTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Tests\Unit;
  3. use Tests\TestCase;
  4. class ProfileObjectTest extends TestCase
  5. {
  6. public function testLibraryInstalled()
  7. {
  8. $this->assertTrue(class_exists('\Activitypub_profile'));
  9. }
  10. public function testActivitypubProfile()
  11. {
  12. // Mimic proper ACCEPT header
  13. $_SERVER['HTTP_ACCEPT'] = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams';
  14. /* Test do_insert() */
  15. $aprofile = new \Activitypub_profile();
  16. $aprofile->uri = 'https://testinstance.net/index.php/user/1';
  17. $aprofile->nickname = 'test1';
  18. $aprofile->fullname = 'Test User 1';
  19. $aprofile->bio = 'I am a nice test 1 guy';
  20. $aprofile->inboxuri = "https://testinstance.net/index.php/user/1/inbox.json";
  21. $aprofile->sharedInboxuri = "https://testinstance.net/inbox.json";
  22. $aprofile->do_insert();
  23. /* Test local_profile() */
  24. $profile = $aprofile->local_profile();
  25. /* Test from_profile() and create_from_local_profile() */
  26. $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::from_profile($profile)));
  27. /* Create Keys for Test User 1 */
  28. $apRSA = new \Activitypub_rsa();
  29. $apRSA->profile_id = $profile->getID();
  30. \Activitypub_rsa::generate_keys($apRSA->private_key, $apRSA->public_key);
  31. $apRSA->store_keys();
  32. /* Test profile_to_array() */
  33. // Fetch ActivityPub Actor Object representation
  34. $profile_array = \Activitypub_profile::profile_to_array($profile);
  35. // Check type
  36. $this->assertTrue(is_array($profile_array));
  37. // Test with Explorer's Profile Tester
  38. $this->assertTrue(\Activitypub_explorer::validate_remote_response($profile_array));
  39. /* Test get_inbox() */
  40. $this->assertTrue($aprofile->sharedInboxuri == $aprofile->get_inbox());
  41. /* Test getUri() */
  42. $this->assertTrue($aprofile->uri == $aprofile->getUri());
  43. /* Test getUrl() */
  44. $this->assertTrue($profile->getUrl() == $aprofile->getUrl());
  45. /* Test getID() */
  46. $this->assertTrue($profile->getID() == $aprofile->getID());
  47. /* Test fromUri() */
  48. $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::fromUri($aprofile->uri)));
  49. /* Remove Remote User Test 1 */
  50. $old_id = $profile->getID();
  51. $apRSA->delete();
  52. $aprofile->delete();
  53. $profile->delete();
  54. // Check if successfuly removed
  55. try {
  56. \Profile::getById($old_id);
  57. $this->assertTrue(false);
  58. } catch (\NoResultException $e) {
  59. $this->assertTrue(true);
  60. }
  61. /* Test ensure_web_finger() */
  62. // TODO: Maybe elaborate on this function's tests
  63. try {
  64. \Activitypub_profile::ensure_web_finger('test1@testinstance.net');
  65. $this->assertTrue(false);
  66. } catch (\Exception $e) {
  67. $this->assertTrue($e->getMessage() == 'Not a valid webfinger address.' ||
  68. $e->getMessage() == 'Not a valid webfinger address (via cache).');
  69. }
  70. }
  71. // Helpers
  72. private function compare_profiles(\Profile $a, \Profile $b)
  73. {
  74. if (($av = $a->getID()) != ($bv = $b->getID())) {
  75. throw new Exception('Compare Profiles 1 Fail: $a: '.$av.' is different from $b: '.$bv);
  76. }
  77. if (($av = $a->getNickname()) != ($bv = $b->getNickname())) {
  78. throw new Exception('Compare Profiles 2 Fail: $a: '.$av.' is different from $b: '.$bv);
  79. }
  80. if (($av = $a->getFullname()) != ($bv = $b->getFullname())) {
  81. throw new Exception('Compare Profiles 3 Fail: $a: '.$av.' is different from $b: '.$bv);
  82. }
  83. if (($av = $a->getUrl()) != ($bv = $b->getUrl())) {
  84. throw new Exception('Compare Profiles 4 Fail: $a: '.$av.' is different from $b: '.$bv);
  85. }
  86. if (($av = $a->getDescription()) != ($bv = $b->getDescription())) {
  87. throw new Exception('Compare Profiles 5 Fail: $a: '.$av.' is different from $b: '.$bv);
  88. }
  89. if (($av = $a->getLocation()) != ($bv = $b->getLocation())) {
  90. throw new Exception('Compare Profiles 6 Fail: $a: '.$av.' is different from $b: '.$bv);
  91. }
  92. if (($av = $a->getNickname()) != ($bv = $b->getNickname())) {
  93. throw new Exception('Compare Profiles 7 Fail: $a: '.$av.' is different from $b: '.$bv);
  94. }
  95. if (($av = $a->lat) != ($bv = $b->lat)) {
  96. throw new Exception('Compare Profiles 8 Fail: $a: '.$av.' is different from $b: '.$bv);
  97. }
  98. if (($av = $a->lon) != ($bv = $b->lon)) {
  99. throw new Exception('Compare Profiles 9 Fail: $a: '.$av.' is different from $b: '.$bv);
  100. }
  101. return true;
  102. }
  103. private function compare_aprofiles(\Activitypub_profile $a, \Activitypub_profile $b)
  104. {
  105. if (($av = $a->getUri()) != ($bv = $b->getUri())) {
  106. throw new Exception('Compare AProfiles 1 Fail: $a: '.$av.' is different from $b: '.$bv);
  107. }
  108. if (($av = $a->getUrl()) != ($bv = $b->getUrl())) {
  109. throw new Exception('Compare AProfiles 2 Fail: $a: '.$av.' is different from $b: '.$bv);
  110. }
  111. if (($av = $a->getID()) != ($bv = $b->getID())) {
  112. throw new Exception('Compare AProfiles 3 Fail: $a: '.$av.' is different from $b: '.$bv);
  113. }
  114. if (($av = $a->profile_id) != ($bv = $b->profile_id)) {
  115. throw new Exception('Compare AProfiles 4 Fail: $a: '.$av.' is different from $b: '.$bv);
  116. }
  117. if (($av = $a->inboxuri) != ($bv = $b->inboxuri)) {
  118. throw new Exception('Compare AProfiles 5 Fail: $a: '.$av.' is different from $b: '.$bv);
  119. }
  120. if (($av = $a->sharedInboxuri) != ($bv = $b->sharedInboxuri)) {
  121. throw new Exception('Compare AProfiles 6 Fail: $a: '.$av.' is different from $b: '.$bv);
  122. }
  123. return true;
  124. }
  125. }