1 # -*- encoding: utf-8 -*-
5 describe SpeechBalloon do
7 @admin = FactoryGirl.create :admin
8 @user = FactoryGirl.create( :user_yas)
9 @author = FactoryGirl.create :author, :user_id => @user.id
10 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11 @other_user = FactoryGirl.create( :user_yas)
12 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
13 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
14 @sp = FactoryGirl.create :system_picture
15 @lg = FactoryGirl.create :license_group
16 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
18 @panel = FactoryGirl.create :panel, :author_id => @author.id
19 @writing_format = FactoryGirl.create :writing_format
20 @speech_balloon_template = FactoryGirl.create :speech_balloon_template
25 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
28 context 'オーソドックスなデータのとき' do
36 @sb.classname = 'a'*50
43 context 'panel_idを検証するとき' do
44 #ネストの保存はnilを許可しなければならないので数値チェックだけ
47 @sb.should_not be_valid
50 context 'speech_balloon_template_idを検証するとき' do
52 @sb.speech_balloon_template_id = nil
53 @sb.should_not be_valid
56 @sb.speech_balloon_template_id = 'a'
57 @sb.should_not be_valid
59 it '存在するフキダシテンプレートでなければ失敗する' do
60 @sb.speech_balloon_template_id = 0
61 @sb.should_not be_valid
64 context 'classnameを検証するとき' do
67 @sb.should_not be_valid
70 @sb.classname = 'a'*51
71 @sb.should_not be_valid
77 @sb.should_not be_valid
81 @sb.should_not be_valid
85 @sb.should_not be_valid
88 context 'settingsを検証するとき' do
92 describe '文字コード検証に於いて' do
94 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
97 context 'classnameを検証するとき' do
98 it 'Shift JISなら失敗する' do
99 @sb.classname = "\x83G\x83r\x83]\x83D"
102 }.should raise_error(Pettanr::BadRequest)
106 context 'settingsを検証するとき' do
107 it 'Shift JISなら失敗する' do
108 @sb.settings = "\x83G\x83r\x83]\x83D"
111 }.should raise_error(Pettanr::BadRequest)
115 context 'captionを検証するとき' do
116 it 'Shift JISなら失敗する' do
117 @sb.caption = "\x83G\x83r\x83]\x83D"
120 }.should raise_error(Pettanr::BadRequest)
126 describe '閲覧許可に於いて' do
128 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
130 context 'オープンモードのとき' do
132 MagicNumber['run_mode'] = 0
134 it '自身にゲスト用ロールチェックを問い合わせしている' do
135 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(true)
136 SpeechBalloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
137 r = @sb.visible?([@author])
139 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
140 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(false)
141 r = @sb.visible?([@author])
145 context 'クローズドモードのとき' do
147 MagicNumber['run_mode'] = 1
149 it '自身に読者用ロールチェックを問い合わせしている' do
150 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
151 SpeechBalloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
152 r = @sb.visible?([@author])
154 it '読者用ロールチェックが失敗したとき、falseを返す' do
155 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(false)
156 r = @sb.visible?([@author])
160 context '事前チェックする' do
162 MagicNumber['run_mode'] = 1
163 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
165 it '自身のコマに所持判定を問い合わせしている' do
166 Panel.any_instance.stub(:own?).and_return(true)
167 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
168 r = @sb.visible?([@author])
170 it '自身のコマに閲覧許可を問い合わせしている' do
171 Panel.any_instance.stub(:own?).and_return(false)
172 Panel.any_instance.stub(:visible?).and_return(true)
173 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
174 r = @sb.visible?([@author])
177 context 'つつがなく終わるとき' do
179 MagicNumber['run_mode'] = 1
180 Panel.any_instance.stub(:reader_role_check).and_return(true)
182 it '自分のコマのフキダシなら許可する' do
183 Panel.any_instance.stub(:own?).and_return(true)
184 Panel.any_instance.stub(:visible?).and_return(false)
185 r = @sb.visible?([@author])
188 it '他人の非公開コマのフキダシなら許可しない' do
189 Panel.any_instance.stub(:own?).and_return(false)
190 Panel.any_instance.stub(:visible?).and_return(false)
191 r = @sb.visible?([@author])
194 it '他人のコマのフキダシでも公開なら許可する' do
195 Panel.any_instance.stub(:own?).and_return(false)
196 Panel.any_instance.stub(:visible?).and_return(true)
197 r = @sb.visible?([@author])
203 describe '一覧取得に於いて' do
205 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
206 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
207 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
209 context 'page補正について' do
210 it '文字列から数値に変換される' do
211 SpeechBalloon.page('8').should eq 8
214 SpeechBalloon.page().should eq 1
217 SpeechBalloon.page('0').should eq 1
220 context 'page_size補正について' do
221 it '文字列から数値に変換される' do
222 SpeechBalloon.page_size('7').should eq 7
224 it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
225 SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
227 it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
228 SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
230 it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
231 SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
234 context 'つつがなく終わるとき' do
235 it '一覧取得オプションを利用している' do
236 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
237 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
238 r = SpeechBalloon.list
242 r = SpeechBalloon.list
246 #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
247 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
248 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
249 r = SpeechBalloon.list
250 r.should eq [npl, @sb]
252 it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
253 hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
254 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
255 pl = SpeechBalloon.list
258 context 'DBに5件あって1ページの件数を2件に変えたとして' do
260 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
261 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
262 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
263 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
264 SpeechBalloon.stub(:default_page_size).and_return(2)
267 pl = SpeechBalloon.list
268 pl.should have(2).items
270 it 'page=1なら末尾2件を返す' do
272 pl = SpeechBalloon.list(1)
273 pl.should eq [@sb5, @sb4]
275 it 'page=2なら中間2件を返す' do
276 pl = SpeechBalloon.list(2)
277 pl.should eq [@sb3, @sb2]
279 it 'page=3なら先頭1件を返す' do
280 pl = SpeechBalloon.list(3)
286 describe '自分のフキダシ一覧取得に於いて' do
288 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
289 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
290 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
292 context 'つつがなく終わるとき' do
293 it '一覧取得オプションを利用している' do
294 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
295 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
296 r = SpeechBalloon.mylist @author
300 pl = SpeechBalloon.mylist @author
304 npl = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
305 pl = SpeechBalloon.mylist @author
306 pl.should eq [npl, @sb]
308 it '他人のコマのフキダシは公開コマでも含まない' do
309 hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
310 npl = FactoryGirl.create :speech_balloon, :panel_id => hpl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
311 pl = SpeechBalloon.mylist @author
314 it '自分のコマのフキダシは非公開コマでも含んでいる' do
315 pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
316 ni = FactoryGirl.create :speech_balloon, :panel_id => pl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
317 r = SpeechBalloon.mylist @author
318 r.should eq [ni, @sb]
320 context 'DBに5件あって1ページの件数を2件に変えたとして' do
322 @npl2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
323 @npl3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 2, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 200
324 @npl4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 3, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 300
325 @npl5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 4, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 400
328 r = SpeechBalloon.mylist @author, 1, 2
329 r.should have(2).items
331 it 'page=1なら末尾2件を返す' do
333 r = SpeechBalloon.mylist(@author, 1, 2)
334 r.should eq [@npl5, @npl4]
336 it 'page=2なら中間2件を返す' do
337 r = SpeechBalloon.mylist(@author, 2, 2)
338 r.should eq [@npl3, @npl2]
340 it 'page=3なら先頭1件を返す' do
341 r = SpeechBalloon.mylist(@author, 3, 2)
347 describe '他作家のフキダシ一覧取得に於いて' do
349 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
350 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
351 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
352 @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
353 @other_sb = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
356 r = SpeechBalloon.himlist @other_author
357 r.should eq [@other_sb]
360 new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
361 new_sb = FactoryGirl.create :speech_balloon, :panel_id => new_panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
362 r = SpeechBalloon.himlist @other_author
363 r.should eq [new_sb, @other_sb]
366 hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
367 hidden_sb = FactoryGirl.create :speech_balloon, :panel_id => hidden_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
368 r = SpeechBalloon.himlist @other_author
369 r.should eq [@other_sb]
371 context 'DBに5件あって1ページの件数を2件に変えたとして' do
373 @other_sb2 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
374 @other_sb3 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 2, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 200
375 @other_sb4 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 3, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 300
376 @other_sb5 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 4, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 400
379 r = SpeechBalloon.himlist @other_author, 1, 2
380 r.should have(2).items
382 it 'page=1なら末尾2件を返す' do
384 r = SpeechBalloon.himlist @other_author, 1, 2
385 r.should eq [@other_sb5, @other_sb4]
387 it 'page=2なら中間2件を返す' do
388 r = SpeechBalloon.himlist @other_author, 2, 2
389 r.should eq [@other_sb3, @other_sb2]
391 it 'page=3なら先頭1件を返す' do
392 r = SpeechBalloon.himlist @other_author, 3, 2
393 r.should eq [@other_sb]
398 describe 'フキダシ一覧ページ制御に於いて' do
400 SpeechBalloon.stub(:count).with(any_args).and_return(100)
403 r = SpeechBalloon.list_paginate
404 r.is_a?(Kaminari::PaginatableArray).should be_true
406 it 'フキダシ一覧の取得条件を利用している' do
407 SpeechBalloon.stub(:list_where).with(any_args).and_return('')
408 SpeechBalloon.should_receive(:list_where).with(any_args).exactly(1)
409 r = SpeechBalloon.list_paginate
411 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
412 r = SpeechBalloon.list_paginate 3, 10
413 r.limit_value.should eq 10
414 r.offset_value.should eq 20
418 describe '自分のフキダシ一覧ページ制御に於いて' do
420 SpeechBalloon.stub(:count).with(any_args).and_return(100)
423 r = SpeechBalloon.mylist_paginate @author
424 r.is_a?(Kaminari::PaginatableArray).should be_true
426 it '自分のフキダシ一覧の取得条件を利用している' do
427 SpeechBalloon.stub(:mylist_where).with(any_args).and_return('')
428 SpeechBalloon.should_receive(:mylist_where).with(any_args).exactly(1)
429 r = SpeechBalloon.mylist_paginate @author
431 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
432 r = SpeechBalloon.mylist_paginate @author, 3, 10
433 r.limit_value.should eq 10
434 r.offset_value.should eq 20
438 describe '他作家のフキダシ一覧ページ制御に於いて' do
440 SpeechBalloon.stub(:count).with(any_args).and_return(100)
443 r = SpeechBalloon.himlist_paginate @other_author
444 r.is_a?(Kaminari::PaginatableArray).should be_true
446 it '他作家のフキダシ一覧の取得条件を利用している' do
447 SpeechBalloon.stub(:himlist_where).with(any_args).and_return('')
448 SpeechBalloon.should_receive(:himlist_where).with(any_args).exactly(1)
449 r = SpeechBalloon.himlist_paginate @other_author
451 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
452 r = SpeechBalloon.himlist_paginate @other_author, 3, 10
453 r.limit_value.should eq 10
454 r.offset_value.should eq 20
458 describe '一覧取得オプションに於いて' do
460 r = SpeechBalloon.list_opt
461 r.should have(4).items
464 r = SpeechBalloon.list_opt
465 r.has_key?(:panel).should be_true
468 r = SpeechBalloon.list_opt
469 r[:panel].has_key?(:author).should be_true
472 r = SpeechBalloon.list_opt
473 r.has_key?(:balloon).should be_true
476 r = SpeechBalloon.list_opt
477 r.has_key?(:speech).should be_true
479 it 'フキダシテンプレートを含んでいる' do
480 r = SpeechBalloon.list_opt
481 r.has_key?(:speech_balloon_template).should be_true
484 describe 'json一覧出力オプションに於いて' do
486 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
487 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
488 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
489 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
492 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
495 i.has_key?('panel').should be_true
498 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
502 s.has_key?('author').should be_true
505 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
508 i.has_key?('balloon').should be_true
511 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
514 i.has_key?('speech').should be_true
516 it 'フキダシテンプレートを含んでいる' do
517 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
520 i.has_key?('speech_balloon_template').should be_true
524 describe '単体取得に於いて' do
526 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
527 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
528 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
530 context 'つつがなく終わるとき' do
531 it '単体取得オプションを利用している' do
532 SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
533 SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
534 r = SpeechBalloon.show @sb.id, @author
536 it '閲覧許可を問い合わせている' do
537 SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
538 SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
539 r = SpeechBalloon.show @sb.id, @author
543 SpeechBalloon.any_instance.stub(:visible?).and_return(true)
544 pl = SpeechBalloon.show @sb.id, @author
547 context '閲覧許可が出なかったとき' do
548 it '403Forbidden例外を返す' do
549 SpeechBalloon.any_instance.stub(:visible?).and_return(false)
551 SpeechBalloon.show @sb.id, @author
552 }.should raise_error(ActiveRecord::Forbidden)
555 context '存在しないフキダシを開こうとしたとき' do
556 it '404RecordNotFound例外を返す' do
558 SpeechBalloon.show 110, @author
559 }.should raise_error(ActiveRecord::RecordNotFound)
563 describe '単体取得オプションに於いて' do
564 it 'includeキーを含んでいる' do
565 r = SpeechBalloon.show_opt
566 r.has_key?(:include).should be_true
569 r = SpeechBalloon.show_opt[:include]
570 r.should have(4).items
573 r = SpeechBalloon.show_opt[:include]
574 r.has_key?(:panel).should be_true
577 r = SpeechBalloon.show_opt[:include]
578 r[:panel].has_key?(:author).should be_true
581 r = SpeechBalloon.show_opt[:include]
582 r.has_key?(:balloon).should be_true
585 r = SpeechBalloon.show_opt[:include]
586 r.has_key?(:speech).should be_true
588 it 'フキダシテンプレートを含んでいる' do
589 r = SpeechBalloon.show_opt[:include]
590 r.has_key?(:speech_balloon_template).should be_true
593 describe 'json単体出力オプションに於いて' do
595 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
596 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
597 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
598 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
601 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
604 i.has_key?('panel').should be_true
607 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
611 s.has_key?('author').should be_true
614 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
617 i.has_key?('balloon').should be_true
620 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
623 i.has_key?('speech').should be_true
625 it 'フキダシテンプレートを含んでいる' do
626 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
629 i.has_key?('speech_balloon_template').should be_true