SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/embed_videoTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\EmbedVideo;
  15.  
  16.  
  17. class embed_videoTest extends PHPUnit_Framework_TestCase
  18. {
  19.  
  20. public function testGetVideoIdType()
  21. {
  22. // Youtube
  23. $out = EmbedVideo::getVideoIdType('http://www.youtube.com/watch?v=YP31r70_QNM&feature=grec_index');
  24. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  25. $this->assertTrue($out[1] == 'YP31r70_QNM');
  26.  
  27. $out = EmbedVideo::getVideoIdType('http://youtu.be/YP31r70_QNM');
  28. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  29. $this->assertTrue($out[1] == 'YP31r70_QNM');
  30.  
  31. $out = EmbedVideo::getVideoIdType('https://youtu.be/YP31r70_QNM');
  32. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  33. $this->assertTrue($out[1] == 'YP31r70_QNM');
  34.  
  35. $out = EmbedVideo::getVideoIdType('http://www.youtube.com/watch?v=c6jWWfQJt78&feature=grec_index');
  36. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  37. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  38.  
  39. $out = EmbedVideo::getVideoIdType('http://www.youtube.com/watch?feature=grec_index&v=c6jWWfQJt78');
  40. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  41. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  42.  
  43. $out = EmbedVideo::getVideoIdType('http://youtube.com/watch?v=c6jWWfQJt78');
  44. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  45. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  46.  
  47. $out = EmbedVideo::getVideoIdType('http://youtube.com/watch?v=c6jWWfQJt78');
  48. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  49. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  50.  
  51. $out = EmbedVideo::getVideoIdType('http://youtu.be/c6jWWfQJt78');
  52. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  53. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  54.  
  55. $out = EmbedVideo::getVideoIdType('https://youtu.be/c6jWWfQJt78');
  56. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  57. $this->assertTrue($out[1] == 'c6jWWfQJt78');
  58.  
  59. $out = EmbedVideo::getVideoIdType('http://youtu.be/dK-Wy2NhO8o');
  60. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  61. $this->assertTrue($out[1] == 'dK-Wy2NhO8o');
  62.  
  63. $out = EmbedVideo::getVideoIdType('http://www.youtube.com/v/4_gMar4_Jhg?version=3&f=videos&app=youtube_gdata&rel=0');
  64. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  65. $this->assertTrue($out[1] == '4_gMar4_Jhg');
  66.  
  67. $out = EmbedVideo::getVideoIdType('https://www.youtube.com/v/4_gMar4_Jhg?version=3&f=videos&app=youtube_gdata&rel=0');
  68. $this->assertTrue($out[0] == EmbedVideo::TYPE_YOUTUBE);
  69. $this->assertTrue($out[1] == '4_gMar4_Jhg');
  70.  
  71. $out = EmbedVideo::getVideoIdType('http://www.youtube.com/user/inyaka');
  72. $this->assertNull($out);
  73.  
  74. $out = EmbedVideo::getVideoIdType('http://youtube.com/user/inyaka');
  75. $this->assertNull($out);
  76.  
  77.  
  78. // Vimeo
  79. $out = EmbedVideo::getVideoIdType('http://vimeo.com/6745866');
  80. $this->assertTrue($out[0] == EmbedVideo::TYPE_VIMEO);
  81. $this->assertTrue($out[1] == '6745866');
  82.  
  83. $out = EmbedVideo::getVideoIdType('https://vimeo.com/6745866');
  84. $this->assertTrue($out[0] == EmbedVideo::TYPE_VIMEO);
  85. $this->assertTrue($out[1] == '6745866');
  86.  
  87. $out = EmbedVideo::getVideoIdType('http://vimeo.com/');
  88. $this->assertNull($out);
  89.  
  90. $out = EmbedVideo::getVideoIdType('http://vimeo.com/channels/staffpicks#13906628');
  91. $this->assertNull($out);
  92.  
  93.  
  94. // other
  95. $out = EmbedVideo::getVideoIdType('http://thejosh.info');
  96. $this->assertNull($out);
  97.  
  98. $out = EmbedVideo::getVideoIdType('abcde');
  99. $this->assertNull($out);
  100.  
  101. $out = EmbedVideo::getVideoIdType(array());
  102. $this->assertNull($out);
  103. }
  104.  
  105.  
  106. public function testRenderEmbed()
  107. {
  108. // Three video urls
  109. $out = EmbedVideo::renderEmbed('http://www.youtube.com/watch?v=YP31r70_QNM&feature=grec_index');
  110. $this->assertContains('<iframe', $out);
  111. $this->assertContains('youtube', $out);
  112. $this->assertContains('400', $out);
  113. $this->assertContains('300', $out);
  114.  
  115. $out = EmbedVideo::renderEmbed('http://youtu.be/YP31r70_QNM');
  116. $this->assertContains('<iframe', $out);
  117. $this->assertContains('youtube', $out);
  118. $this->assertContains('400', $out);
  119. $this->assertContains('300', $out);
  120.  
  121. $out = EmbedVideo::renderEmbed('http://vimeo.com/6745866');
  122. $this->assertContains('<iframe', $out);
  123. $this->assertContains('vimeo', $out);
  124. $this->assertContains('400', $out);
  125. $this->assertContains('300', $out);
  126.  
  127. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866');
  128. $this->assertContains('<iframe', $out);
  129. $this->assertContains('vimeo', $out);
  130. $this->assertContains('400', $out);
  131. $this->assertContains('300', $out);
  132.  
  133.  
  134. // Invalid urls
  135. $out = EmbedVideo::renderEmbed('http://thejosh.info');
  136. $this->assertNull($out);
  137.  
  138. $out = EmbedVideo::renderEmbed(array());
  139. $this->assertNull($out);
  140.  
  141.  
  142. // Sizes
  143. $out = EmbedVideo::renderEmbed('http://youtu.be/YP31r70_QNM', 500, 1000);
  144. $this->assertContains('500', $out);
  145. $this->assertContains('1000', $out);
  146. }
  147.  
  148.  
  149. public function testRenderEmbedAutoplay()
  150. {
  151. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000);
  152. $this->assertNotContains('autoplay=1', $out);
  153.  
  154. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array());
  155. $this->assertNotContains('autoplay=1', $out);
  156.  
  157. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array('autoplay' => false));
  158. $this->assertNotContains('autoplay=1', $out);
  159.  
  160. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array('autoplay' => true));
  161. $this->assertContains('autoplay=1', $out);
  162. }
  163.  
  164.  
  165. public function testRenderEmbedTitle()
  166. {
  167. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000);
  168. $this->assertContains('title="Embedded video"', $out);
  169.  
  170. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array('title' => 'test'));
  171. $this->assertContains('title="test"', $out);
  172.  
  173. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array('title' => null));
  174. $this->assertContains('title="Embedded video"', $out);
  175.  
  176. $out = EmbedVideo::renderEmbed('https://vimeo.com/6745866', 500, 1000, array('title' => ''));
  177. $this->assertContains('title="Embedded video"', $out);
  178. }
  179.  
  180.  
  181. /**
  182.   * @slow
  183.   **/
  184. public function testGetThumbFilename()
  185. {
  186. $out = EmbedVideo::getThumbFilename('http://www.youtube.com/watch?v=YP31r70_QNM&feature=grec_index');
  187. if ($out !== null) {
  188. $this->assertRegExp('!^//!', $out);
  189. $this->assertContains('ytimg', $out);
  190. $size = @getimagesize($out);
  191. $this->assertTrue($size !== null);
  192. }
  193.  
  194. $out = EmbedVideo::getThumbFilename('http://youtu.be/YP31r70_QNM');
  195. if ($out !== null) {
  196. $this->assertRegExp('!^//!', $out);
  197. $this->assertContains('ytimg', $out);
  198. $size = @getimagesize($out);
  199. $this->assertTrue($size !== null);
  200. }
  201.  
  202. $out = EmbedVideo::getThumbFilename('http://vimeo.com/6745866');
  203. if ($out !== null) {
  204. $this->assertRegExp('!^https?://!', $out);
  205. $this->assertContains('vimeo', $out);
  206. $size = @getimagesize($out);
  207. $this->assertTrue($size !== null);
  208. }
  209.  
  210. $out = EmbedVideo::getThumbFilename('dsfsjfsfbnsfbshjfb');
  211. $this->assertNull($out);
  212. }
  213. }
  214.