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.

324 lines
17 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class InstagramTest extends PHPUnit\Framework\TestCase
  5. {
  6. private $http;
  7. public function setUp(): void
  8. {
  9. $this->client = new Parse();
  10. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  11. $this->client->mc = null;
  12. }
  13. private function parse($params)
  14. {
  15. $request = new Request($params);
  16. $response = new Response();
  17. return $this->client->parse($request, $response);
  18. }
  19. public function testInstagramPhoto()
  20. {
  21. // Original URL: https://www.instagram.com/p/BO5rYVElvJq/
  22. $url = 'https://www.instagram.com/p/BO5rYVElvJq/';
  23. $response = $this->parse(['url' => $url]);
  24. $body = $response->getContent();
  25. $this->assertEquals(200, $response->getStatusCode());
  26. $data = json_decode($body, true);
  27. $this->assertEquals(200, $data['code']);
  28. $this->assertEquals('instagram', $data['source-format']);
  29. $this->assertEquals('entry', $data['data']['type']);
  30. $this->assertEquals('photo', $data['data']['post-type']);
  31. $this->assertEquals('2017-01-05T23:31:32+00:00', $data['data']['published']);
  32. $this->assertContains('planning', $data['data']['category']);
  33. $this->assertContains('2017', $data['data']['category']);
  34. $this->assertEquals('Kind of crazy to see the whole year laid out like this. #planning #2017', $data['data']['content']['text']);
  35. $this->assertEquals(1, count($data['data']['photo']));
  36. $this->assertEquals(['https://instagram.fsjc1-3.fna.fbcdn.net/vp/af9471f885e6197478d71807a7cbf297/5CBA6E5F/t51.2885-15/e35/15803256_1832278043695907_4846092951052353536_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net'], $data['data']['photo']);
  37. $this->assertEquals('https://www.instagram.com/aaronpk/', $data['data']['author']['url']);
  38. $this->assertEquals('Aaron Parecki', $data['data']['author']['name']);
  39. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/a2909937316893f18760f1077ca88fa1/5CBB520A/t51.2885-19/s150x150/14240576_268350536897085_1129715662_a.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['author']['photo']);
  40. $this->assertArrayNotHasKey('meta', $data['data']); // make sure this does not include alt text (autogenerated placeholder from instagram)
  41. }
  42. public function testBGDpqNoiMJ0()
  43. {
  44. // https://www.instagram.com/p/BGDpqNoiMJ0/
  45. $url = 'http://www.instagram.com/BGDpqNoiMJ0';
  46. $response = $this->parse(['url' => $url]);
  47. $body = $response->getContent();
  48. $this->assertEquals(200, $response->getStatusCode());
  49. $data = json_decode($body, true);
  50. $this->assertEquals(200, $data['code']);
  51. $this->assertEquals('instagram', $data['source-format']);
  52. $this->assertEquals('entry', $data['data']['type']);
  53. $this->assertEquals('photo', $data['data']['post-type']);
  54. $this->assertSame(
  55. [
  56. 'type' => 'card',
  57. 'name' => 'pk_spam',
  58. 'nickname' => 'pk_spam',
  59. 'url' => 'https://www.instagram.com/pk_spam/',
  60. 'photo' => 'https://scontent-frx5-1.cdninstagram.com/vp/f17e1275a70fc32e93cbf434ddc32bcd/5B6CCC7A/t51.2885-19/11906329_960233084022564_1448528159_a.jpg',
  61. ], $data['data']['author']
  62. );
  63. $this->assertSame(
  64. [
  65. 'muffins',
  66. 'https://www.instagram.com/indiewebcat/'
  67. ], $data['data']['category']
  68. );
  69. $this->assertEquals('Meow #muffins', $data['data']['content']['text']);
  70. $this->assertSame(['https://instagram.fsea1-1.fna.fbcdn.net/vp/9433ea494a8b055bebabf70fd81cfa32/5B51F092/t51.2885-15/e35/13266755_877794672348882_1908663476_n.jpg'], $data['data']['photo']);
  71. $this->assertEquals('2016-05-30T20:46:22-07:00', $data['data']['published']);
  72. $this->assertEquals('https://www.instagram.com/explore/locations/359000003/', $data['data']['location'][0]);
  73. $this->assertSame(
  74. [
  75. 'type' => 'card',
  76. 'name' => 'Burnside 26',
  77. 'url' => 'https://www.instagram.com/explore/locations/359000003/',
  78. 'latitude' => 45.52322,
  79. 'longitude' => -122.63885
  80. ], $data['data']['refs']['https://www.instagram.com/explore/locations/359000003/']
  81. );
  82. }
  83. public function testInstagramVideo()
  84. {
  85. // Original URL: https://www.instagram.com/p/BO_RN8AFZSx/
  86. $url = 'https://www.instagram.com/p/BO_RN8AFZSx/';
  87. $response = $this->parse(['url' => $url]);
  88. $body = $response->getContent();
  89. $this->assertEquals(200, $response->getStatusCode());
  90. $data = json_decode($body, true);
  91. $this->assertEquals(200, $data['code']);
  92. $this->assertEquals('instagram', $data['source-format']);
  93. $this->assertEquals('entry', $data['data']['type']);
  94. $this->assertEquals('video', $data['data']['post-type']);
  95. $this->assertContains('100daysofmusic', $data['data']['category']);
  96. $this->assertEquals('Day 18. Maple and Spruce #100daysofmusic #100daysproject #the100dayproject https://aaronparecki.com/2017/01/07/14/day18', $data['data']['content']['text']);
  97. $this->assertEquals(1, count($data['data']['photo']));
  98. $this->assertEquals(['https://instagram.fsjc1-3.fna.fbcdn.net/vp/a77f8672f977413d2eb5239cd6d5c4cf/5C3A4ADF/t51.2885-15/e15/15624670_548881701986735_8264383763249627136_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net'], $data['data']['photo']);
  99. $this->assertEquals(1, count($data['data']['video']));
  100. $this->assertEquals(['https://instagram.fsjc1-3.fna.fbcdn.net/vp/90ed8fe576cba16e258c0f4cfc05299a/5C3A129E/t50.2886-16/15921147_1074837002642259_2269307616507199488_n.mp4?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net'], $data['data']['video']);
  101. $this->assertEquals('https://www.instagram.com/aaronpk/', $data['data']['author']['url']);
  102. $this->assertEquals('Aaron Parecki', $data['data']['author']['name']);
  103. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/a2909937316893f18760f1077ca88fa1/5CBB520A/t51.2885-19/s150x150/14240576_268350536897085_1129715662_a.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['author']['photo']);
  104. }
  105. public function testInstagramPhotoWithPersonTag()
  106. {
  107. // Original URL: https://www.instagram.com/p/BNfqVfVlmkj/
  108. $url = 'https://www.instagram.com/p/BNfqVfVlmkj/';
  109. $response = $this->parse(['url' => $url]);
  110. $body = $response->getContent();
  111. $this->assertEquals(200, $response->getStatusCode());
  112. $data = json_decode($body, true);
  113. $this->assertEquals(200, $data['code']);
  114. $this->assertEquals('instagram', $data['source-format']);
  115. $this->assertEquals(2, count($data['data']['category']));
  116. $this->assertEquals(['type'=>'card','name'=>'KmikeyM™️','nickname'=>'kmikeym','url'=>'https://www.instagram.com/kmikeym/','photo'=>'https://instagram.fsjc1-3.fna.fbcdn.net/vp/ea5b988b616dbcc778b3013bf2426d70/5CCAC7FC/t51.2885-19/s320x320/20634957_814691788710973_2275383796935163904_a.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net','note'=>"The world’s first publicly traded person.\n\nAcct in collaboration with @norbertoinc\n\nBecome a shareholder today!\n"], $data['data']['refs']['https://www.instagram.com/kmikeym/']);
  117. $this->assertContains('https://www.instagram.com/kmikeym/', $data['data']['category']);
  118. $this->assertArrayHasKey('https://www.instagram.com/kmikeym/', $data['data']['refs']);
  119. }
  120. public function testInstagramPhotoWithVenue()
  121. {
  122. // Original URL: https://www.instagram.com/p/BN3Z5salSys/
  123. $url = 'https://www.instagram.com/p/BN3Z5salSys/';
  124. $response = $this->parse(['url' => $url]);
  125. $body = $response->getContent();
  126. $this->assertEquals(200, $response->getStatusCode());
  127. $data = json_decode($body, true);
  128. $this->assertEquals(200, $data['code']);
  129. $this->assertEquals('instagram', $data['source-format']);
  130. $this->assertEquals(1, count($data['data']['location']));
  131. $this->assertContains('https://www.instagram.com/explore/locations/109284789535230/', $data['data']['location']);
  132. $this->assertArrayHasKey('https://www.instagram.com/explore/locations/109284789535230/', $data['data']['refs']);
  133. $venue = $data['data']['refs']['https://www.instagram.com/explore/locations/109284789535230/'];
  134. $this->assertEquals('XOXO Outpost', $venue['name']);
  135. $this->assertEquals('45.5261002', $venue['latitude']);
  136. $this->assertEquals('-122.6558081', $venue['longitude']);
  137. // Setting a venue should set the timezone
  138. $this->assertEquals('2016-12-10T21:48:56-08:00', $data['data']['published']);
  139. }
  140. public function testTwoPhotos()
  141. {
  142. // Original URL: https://www.instagram.com/p/BZWmUB_DVtp/
  143. $url = 'https://www.instagram.com/p/BZWmUB_DVtp/';
  144. $response = $this->parse(['url' => $url]);
  145. $body = $response->getContent();
  146. $this->assertEquals(200, $response->getStatusCode());
  147. $data = json_decode($body, true);
  148. $this->assertEquals(200, $data['code']);
  149. $this->assertEquals('instagram', $data['source-format']);
  150. $this->assertEquals(2, count($data['data']['photo']));
  151. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/6b09c3d5490ee3efb55849858a9ec014/5CBFBC38/t51.2885-15/e35/21827424_134752690591737_8093088291252862976_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['photo'][0]);
  152. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/8b1b2e6efa86a4856ec37a60f0fa77f5/5CC2D34D/t51.2885-15/e35/21909774_347707439021016_5237540582556958720_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['photo'][1]);
  153. $this->assertArrayNotHasKey('video', $data['data']);
  154. $this->assertEquals(2, count($data['data']['category']));
  155. $this->assertArrayNotHasKey('meta', $data['data']);
  156. }
  157. public function testMixPhotosAndVideos()
  158. {
  159. // Original URL: https://www.instagram.com/p/BZWmpecjBwN/
  160. $url = 'https://www.instagram.com/p/BZWmpecjBwN/';
  161. $response = $this->parse(['url' => $url]);
  162. $body = $response->getContent();
  163. $this->assertEquals(200, $response->getStatusCode());
  164. $data = json_decode($body, true);
  165. $this->assertEquals(200, $data['code']);
  166. $this->assertEquals('instagram', $data['source-format']);
  167. $this->assertEquals('photo', $data['data']['post-type']); // we discard videos in this case right now
  168. $this->assertEquals(3, count($data['data']['photo']));
  169. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/ee1a28763918069f3e54dad35be24ad8/5CCFBAB8/t51.2885-15/e35/21878922_686481254874005_8468823712617988096_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['photo'][0]);
  170. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/ddc0ebe969bb1f9e6bf8adada0892c90/5C39EBC9/t51.2885-15/e15/21910026_1507234999368159_6974261907783942144_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['photo'][1]);
  171. $this->assertEquals('https://instagram.fsjc1-3.fna.fbcdn.net/vp/bfe032af795427443ea448840df1c3a4/5CCC8C88/t51.2885-15/e35/21878800_273567963151023_7672178549897297920_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net', $data['data']['photo'][2]);
  172. $this->assertArrayNotHasKey('video', $data['data']);
  173. $this->assertEquals(2, count($data['data']['category']));
  174. }
  175. public function testInstagramProfile()
  176. {
  177. $url = 'https://www.instagram.com/aaronpk/';
  178. $response = $this->parse(['url' => $url]);
  179. $body = $response->getContent();
  180. $this->assertEquals(200, $response->getStatusCode());
  181. $data = json_decode($body, true);
  182. $this->assertEquals(200, $data['code']);
  183. $this->assertEquals('instagram', $data['source-format']);
  184. $this->assertSame(
  185. [
  186. 'type' => 'card',
  187. 'name' => 'Aaron Parecki',
  188. 'nickname' => 'aaronpk',
  189. 'url' => 'https://www.instagram.com/aaronpk/',
  190. 'photo' => 'https://instagram.fsjc1-3.fna.fbcdn.net/vp/45aee453740a714bf408f8947f89da8e/5CCB4B8E/t51.2885-19/s320x320/14240576_268350536897085_1129715662_a.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net',
  191. 'note' => '🔒 oauth.net 🎥 backpedal.tv 🎙 streampdx.com 📡 w7apk.com'
  192. ], $data['data']
  193. );
  194. }
  195. public function testInstagramProfileWithBio()
  196. {
  197. $url = 'https://www.instagram.com/pk_spam/';
  198. $response = $this->parse(['url' => $url]);
  199. $body = $response->getContent();
  200. $this->assertEquals(200, $response->getStatusCode());
  201. $data = json_decode($body, true);
  202. $this->assertEquals(200, $data['code']);
  203. $this->assertEquals('instagram', $data['source-format']);
  204. $this->assertSame(
  205. [
  206. 'type' => 'card',
  207. 'name' => 'pk_spam',
  208. 'nickname' => 'pk_spam',
  209. 'url' => 'https://www.instagram.com/pk_spam/',
  210. 'photo' => 'https://scontent-frx5-1.cdninstagram.com/vp/74112f515c64726429c69fedcb927c2d/5CB64CF1/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com',
  211. 'note' => 'My website is https://aaronparecki.com.dev/ and http://aaronpk.micro.blog/about/ and https://tiny.xyz.dev/'
  212. ], $data['data']
  213. );
  214. }
  215. public function testInstagramProfileFeed()
  216. {
  217. $url = 'https://www.instagram.com/pk_spam/';
  218. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  219. $body = $response->getContent();
  220. $this->assertEquals(200, $response->getStatusCode());
  221. $data = json_decode($body, true);
  222. $this->assertEquals(200, $data['code']);
  223. $this->assertEquals('instagram', $data['source-format']);
  224. $this->assertEquals('feed', $data['data']['type']);
  225. $this->assertEquals(12, count($data['data']['items']));
  226. $this->assertEquals('https://www.instagram.com/p/BsdlOmLh_IX/', $data['data']['items'][0]['url']);
  227. $this->assertEquals('https://www.instagram.com/p/BGFdtAViMJy/', $data['data']['items'][11]['url']);
  228. }
  229. public function testInstagramPhotoWithAltText()
  230. {
  231. $url = 'https://www.instagram.com/p/BsdjKytBZyx/';
  232. $response = $this->parse(['url' => $url]);
  233. $body = $response->getContent();
  234. $this->assertEquals(200, $response->getStatusCode());
  235. $data = json_decode($body, true);
  236. $this->assertEquals(200, $data['code']);
  237. $this->assertEquals('instagram', $data['source-format']);
  238. $this->assertEquals('Pink text on a white background that says "Photo with alt text"', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/a7e61adf3d84f07863ffdb99f0fdcc86/5CD9B7F3/t51.2885-15/e35/47692478_2276538359047529_8318084305806697090_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
  239. }
  240. public function testInstagramMultiPhotoWithAltText()
  241. {
  242. $url = 'https://www.instagram.com/p/BsdlOmLh_IX/';
  243. $response = $this->parse(['url' => $url]);
  244. $body = $response->getContent();
  245. $this->assertEquals(200, $response->getStatusCode());
  246. $data = json_decode($body, true);
  247. $this->assertEquals(200, $data['code']);
  248. $this->assertEquals('instagram', $data['source-format']);
  249. $this->assertEquals('A large pink "1" in a circle with a small green "2" behind it', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/90bf019b7396d7bc2b1ee02170902a2e/5CCC9B87/t51.2885-15/e35/47692921_321791688431421_3314633848293773579_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
  250. $this->assertEquals('A large green "2" in a circle with a small pink "1" behind it', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/a6c93d8fcd5ad0e3b60f2ac0695eb34e/5CC3898E/t51.2885-15/e35/49663055_349750985612151_2949260446582336214_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
  251. }
  252. public function testInstagramPhotoAutogeneratedAltText()
  253. {
  254. $url = 'https://www.instagram.com/p/Bq8U12UAcdq/';
  255. $response = $this->parse(['url' => $url]);
  256. $body = $response->getContent();
  257. $this->assertEquals(200, $response->getStatusCode());
  258. $data = json_decode($body, true);
  259. $this->assertEquals(200, $data['code']);
  260. $this->assertEquals('instagram', $data['source-format']);
  261. $this->assertEquals('Image may contain: one or more people and hat', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/7f8954f33de897c0c57656b798637f4c/5CC3DF9F/t51.2885-15/e35/45605085_1989380037822519_4707213851165118070_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
  262. }
  263. }