cirandas.net

ref: master

plugins/video/test/unit/video_block_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require_relative '../test_helper'
class VideoBlockTest < ActiveSupport::TestCase

  should "display api_content enabled by default" do
    block = VideoPlugin::VideoBlock.new
    assert block.display_api_content_by_default?
  end

  ### Tests for YouTube

  should "api_content no contains mime-type if platform is youtube" do
    block = VideoPlugin::VideoBlock.new
    block.url = "https://youtube.com/?v=XXXXX"
    assert_includes block.api_content, :url
    assert_includes block.api_content, :video_type
    assert_includes block.api_content, :url_formatted
    refute_includes block.api_content, :mime_type
  end

  should "api_content video_type is youtube" do
    block = VideoPlugin::VideoBlock.new
    block.url = "https://youtube.com/?v=XXXXX"
    assert block.api_content[:video_type] == 'youtube'
    assert_includes block.api_content[:url_formatted], 'www.youtube-nocookie.com'
  end

  should "is_youtube return true when the url contains http://youtube.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://youtube.com/?v=XXXXX"
    assert block.is_youtube?
  end

  should "is_youtube return true when the url contains https://youtube.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "https://youtube.com/?v=XXXXX"
    assert block.is_youtube?
  end

  should "is_youtube return true when the url contains www.youtube.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "www.youtube.com/?v=XXXXX"
    assert block.is_youtube?
  end

  should "is_youtube return true when the url contains youtube.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "youtube.com/?v=XXXXX"
    assert block.is_youtube?
  end

  should "is_youtube return false when the url not contains youtube video ID" do
    block = VideoPlugin::VideoBlock.new
    block.url = "youtube.com/"
    refute block.is_youtube?
  end

  should "is_youtube return false when the url contains empty youtube video ID" do
    block = VideoPlugin::VideoBlock.new
    block.url = "youtube.com/?v="
    refute block.is_youtube?
  end

  should "is_youtube return false when the url contains an invalid youtube link" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.yt.com/?v=XXXXX"
    refute block.is_youtube?
  end

  should "format embed video for youtube videos" do
    block = VideoPlugin::VideoBlock.new
    block.url = "youtube.com/?v=XXXXX"
    assert_match /\/\/www.youtube-nocookie.com\/embed/, block.format_embed_video_url_for_youtube
  end

  should "format embed video return nil if is not a youtube url" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.yt.com/?v=XXXXX"
    assert_nil block.format_embed_video_url_for_youtube
  end

  should "extract youtube id from youtube video url's if it's a valid youtube full url" do
    block = VideoPlugin::VideoBlock.new
    id = 'oi43jre2d2'
    block.url = "youtube.com/?v=#{id}"
    assert_equal id, block.send('extract_youtube_id')
  end

  should "extract youtube id from youtube video url's if it has underline and hyphen" do
    block = VideoPlugin::VideoBlock.new
    id = 'oi43_re-d2'
    block.url = "youtube.com/?v=#{id}"
    assert_equal id, block.send('extract_youtube_id')
  end

  should "extract youtube id from youtube video url's if it's a valid youtube short url" do
    block = VideoPlugin::VideoBlock.new
    id = 'oi43jre2d2'
    block.url = "youtu.be/#{id}"
    assert_equal id, block.send('extract_youtube_id')
  end

  should "extract_youtube_id return nil if the url it's not a valid youtube url" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.yt.com/?v=XXXXX"
    assert_nil block.send('extract_youtube_id')
  end

  should "extract_youtube_id return nil if youtue url there is no id" do
    block = VideoPlugin::VideoBlock.new
    block.url = "youtube.com/"
    assert_nil block.send('extract_youtube_id')
  end

  #### Tests for Vimeo Videos

  should "api_content no contains mime-type if platform is vimeo" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://vimeo.com/98979"
    assert_includes block.api_content, :url
    assert_includes block.api_content, :video_type
    assert_includes block.api_content, :url_formatted
    refute_includes block.api_content, :mime_type
  end

  should "api_content video_type is vimeo" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://vimeo.com/98979"
    assert block.api_content[:video_type] == 'vimeo'
    assert_includes block.api_content[:url_formatted], '//player.vimeo.com/'
  end

  should "is_vimeo return true when the url contains http://vimeo.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://vimeo.com/98979"
    assert block.is_vimeo?
  end

  should "is_vimeo return true when the url contains https://vimeo.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "https://vimeo.com/989798"
    assert block.is_vimeo?
  end

  should "is_vimeo return true when the url contains https://www.vimeo.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "https://www.vimeo.com/98987"
    assert block.is_vimeo?
  end

  should "is_vimeo return true when the url contains www.vimeo.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "www.vimeo.com/989798"
    assert block.is_vimeo?
  end

  should "is_vimeo return true when the url contains vimeo.com" do
    block = VideoPlugin::VideoBlock.new
    block.url = "vimeo.com/09898"
    assert block.is_vimeo?
  end

  should "is_vimeo return false when the url not contains vimeo video ID" do
    block = VideoPlugin::VideoBlock.new
    block.url = "vimeo.com/home"
    refute block.is_vimeo?
  end

  should "is_vimeo return false when the url contains empty vimeo video ID" do
    block = VideoPlugin::VideoBlock.new
    block.url = "vimeo.com/"
    refute block.is_vimeo?
  end

  should "is_vimeo return false when the url contains an invalid vimeo link" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979"
    refute block.is_vimeo?
  end

  should "format embed video for vimeo videos" do
    block = VideoPlugin::VideoBlock.new
    block.url = "vimeo.com/09898"
    assert_match /\/\/player.vimeo.com\/video\/[[:digit:]]+/, block.format_embed_video_url_for_vimeo
  end

  should "format embed video return nil if is not a vimeo url" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.yt.com/?v=XXXXX"
    assert_nil block.format_embed_video_url_for_vimeo
  end

  should "extract vimeo id from vimeo video url's if it's a valid vimeo url" do
    block = VideoPlugin::VideoBlock.new
    id = '23048239432'
    block.url = "vimeo.com/#{id}"
    assert_equal id, block.send('extract_vimeo_id')
  end

  should "extract_vimeo_id return nil if the url it's not a valid vimeo url" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.yt.com/XXXXX"
    assert_nil block.send('extract_vimeo_id')
  end

  should "extract_vimeo_id return nil if vimeo url there is no id" do
    block = VideoPlugin::VideoBlock.new
    block.url = "vimeo.com/"
    assert_nil block.send('extract_youtube_id')
  end

  # Other video formats

  should "api_content contains plaform and mime-type if platform is \'file\'" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.mp4"
    assert_includes block.api_content, :url
    assert_includes block.api_content, :video_type
    assert_includes block.api_content, :url_formatted
    assert_includes block.api_content, :mime_type
  end

  should "api_content video_type is video" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.mp4"
    assert block.api_content[:video_type] == 'video'
    assert_includes block.api_content[:url_formatted], block.url
  end

  should "is_video return true if url ends with mp4" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.mp4"
    assert block.is_video_file?
  end

  should "is_video return true if url ends with ogg" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.ogg"
    assert block.is_video_file?
  end

  should "is_video return true if url ends with ogv" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.ogv"
    assert block.is_video_file?
  end

  should "is_video return true if url ends with webm" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.webm"
    assert block.is_video_file?
  end

  should "is_video return false if url ends without mp4, ogg, ogv, webm" do
    block = VideoPlugin::VideoBlock.new
    block.url = "http://www.vmsd.com/98979.mp4r"
    refute block.is_video_file?
    block.url = "http://www.vmsd.com/98979.oggr"
    refute block.is_video_file?
    block.url = "http://www.vmsd.com/98979.ogvr"
    refute block.is_video_file?
    block.url = "http://www.vmsd.com/98979.webmr"
    refute block.is_video_file?
  end

end