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 @speech_balloon_template = FactoryGirl.create :speech_balloon_template
24 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
27 context 'オーソドックスなデータのとき' do
35 @sb.classname = 'a'*50
42 context 'panel_idを検証するとき' do
43 #ネストの保存はnilを許可しなければならないので数値チェックだけ
46 @sb.should_not be_valid
49 context 'speech_balloon_template_idを検証するとき' do
51 @sb.speech_balloon_template_id = nil
52 @sb.should_not be_valid
55 @sb.speech_balloon_template_id = 'a'
56 @sb.should_not be_valid
58 it '存在するフキダシテンプレートでなければ失敗する' do
59 @sb.speech_balloon_template_id = 0
60 @sb.should_not be_valid
63 context 'classnameを検証するとき' do
66 @sb.should_not be_valid
69 @sb.classname = 'a'*51
70 @sb.should_not be_valid
76 @sb.should_not be_valid
80 @sb.should_not be_valid
84 @sb.should_not be_valid
87 context 'settingsを検証するとき' do
91 describe '文字コード検証に於いて' do
93 @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
96 context 'classnameを検証するとき' do
97 it 'Shift JISなら失敗する' do
98 @sb.classname = "\x83G\x83r\x83]\x83D"
101 }.should raise_error(Pettanr::BadRequest)
105 context 'settingsを検証するとき' do
106 it 'Shift JISなら失敗する' do
107 @sb.settings = "\x83G\x83r\x83]\x83D"
110 }.should raise_error(Pettanr::BadRequest)
114 context 'captionを検証するとき' do
115 it 'Shift JISなら失敗する' do
116 @sb.caption = "\x83G\x83r\x83]\x83D"
119 }.should raise_error(Pettanr::BadRequest)
125 describe '閲覧許可に於いて' do
127 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
129 context 'オープンモードのとき' do
131 MagicNumber['run_mode'] = 0
133 it '自身にゲスト用ロールチェックを問い合わせしている' do
134 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(true)
135 SpeechBalloon.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
136 r = @sb.visible?([@author])
138 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
139 SpeechBalloon.any_instance.stub(:guest_role_check).and_return(false)
140 r = @sb.visible?([@author])
144 context 'クローズドモードのとき' do
146 MagicNumber['run_mode'] = 1
148 it '自身に読者用ロールチェックを問い合わせしている' do
149 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
150 SpeechBalloon.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
151 r = @sb.visible?([@author])
153 it '読者用ロールチェックが失敗したとき、falseを返す' do
154 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(false)
155 r = @sb.visible?([@author])
159 context '事前チェックする' do
161 MagicNumber['run_mode'] = 1
162 SpeechBalloon.any_instance.stub(:reader_role_check).and_return(true)
164 it '自身のコマに所持判定を問い合わせしている' do
165 Panel.any_instance.stub(:own?).and_return(true)
166 Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
167 r = @sb.visible?([@author])
169 it '自身のコマに閲覧許可を問い合わせしている' do
170 Panel.any_instance.stub(:own?).and_return(false)
171 Panel.any_instance.stub(:visible?).and_return(true)
172 Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
173 r = @sb.visible?([@author])
176 context 'つつがなく終わるとき' do
178 MagicNumber['run_mode'] = 1
179 Panel.any_instance.stub(:reader_role_check).and_return(true)
181 it '自分のコマのフキダシなら許可する' do
182 Panel.any_instance.stub(:own?).and_return(true)
183 Panel.any_instance.stub(:visible?).and_return(false)
184 r = @sb.visible?([@author])
187 it '他人の非公開コマのフキダシなら許可しない' do
188 Panel.any_instance.stub(:own?).and_return(false)
189 Panel.any_instance.stub(:visible?).and_return(false)
190 r = @sb.visible?([@author])
193 it '他人のコマのフキダシでも公開なら許可する' do
194 Panel.any_instance.stub(:own?).and_return(false)
195 Panel.any_instance.stub(:visible?).and_return(true)
196 r = @sb.visible?([@author])
202 describe '一覧取得に於いて' do
204 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
205 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
206 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
208 context 'page補正について' do
209 it '文字列から数値に変換される' do
210 SpeechBalloon.page('8').should eq 8
213 SpeechBalloon.page().should eq 1
216 SpeechBalloon.page('0').should eq 1
219 context 'page_size補正について' do
220 it '文字列から数値に変換される' do
221 SpeechBalloon.page_size('7').should eq 7
223 it 'nilの場合はSpeechBalloon.default_page_sizeになる' do
224 SpeechBalloon.page_size().should eq SpeechBalloon.default_page_size
226 it '0以下の場合はSpeechBalloon.default_page_sizeになる' do
227 SpeechBalloon.page_size('0').should eq SpeechBalloon.default_page_size
229 it 'SpeechBalloon.max_page_sizeを超えた場合はSpeechBalloon.max_page_sizeになる' do
230 SpeechBalloon.page_size('1000').should eq SpeechBalloon.max_page_size
233 context 'つつがなく終わるとき' do
234 it '一覧取得オプションを利用している' do
235 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
236 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
237 r = SpeechBalloon.list
241 r = SpeechBalloon.list
245 #公開されたコマのフキダシは(他人のフキダシであっても)含んでいる
246 hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
247 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
248 r = SpeechBalloon.list
249 r.should eq [npl, @sb]
251 it '非公開のコマのフキダシは自分のフキダシであっても含まない' do
252 hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
253 npl = FactoryGirl.create :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
254 pl = SpeechBalloon.list
257 context 'DBに5件あって1ページの件数を2件に変えたとして' do
259 @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
260 @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
261 @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
262 @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
263 SpeechBalloon.stub(:default_page_size).and_return(2)
266 pl = SpeechBalloon.list
267 pl.should have(2).items
269 it 'page=1なら末尾2件を返す' do
271 pl = SpeechBalloon.list(1)
272 pl.should eq [@sb5, @sb4]
274 it 'page=2なら中間2件を返す' do
275 pl = SpeechBalloon.list(2)
276 pl.should eq [@sb3, @sb2]
278 it 'page=3なら先頭1件を返す' do
279 pl = SpeechBalloon.list(3)
285 describe '自分のフキダシ一覧取得に於いて' do
287 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
288 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
289 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
291 context 'つつがなく終わるとき' do
292 it '一覧取得オプションを利用している' do
293 SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
294 SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
295 r = SpeechBalloon.mylist @author
299 pl = SpeechBalloon.mylist @author
303 npl = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
304 pl = SpeechBalloon.mylist @author
305 pl.should eq [npl, @sb]
307 it '他人のコマのフキダシは公開コマでも含まない' do
308 hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
309 npl = FactoryGirl.create :speech_balloon, :panel_id => hpl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
310 pl = SpeechBalloon.mylist @author
313 it '自分のコマのフキダシは非公開コマでも含んでいる' do
314 pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
315 ni = FactoryGirl.create :speech_balloon, :panel_id => pl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
316 r = SpeechBalloon.mylist @author
317 r.should eq [ni, @sb]
319 context 'DBに5件あって1ページの件数を2件に変えたとして' do
321 @npl2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
322 @npl3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 2, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 200
323 @npl4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 3, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 300
324 @npl5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 4, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 400
327 r = SpeechBalloon.mylist @author, 1, 2
328 r.should have(2).items
330 it 'page=1なら末尾2件を返す' do
332 r = SpeechBalloon.mylist(@author, 1, 2)
333 r.should eq [@npl5, @npl4]
335 it 'page=2なら中間2件を返す' do
336 r = SpeechBalloon.mylist(@author, 2, 2)
337 r.should eq [@npl3, @npl2]
339 it 'page=3なら先頭1件を返す' do
340 r = SpeechBalloon.mylist(@author, 3, 2)
346 describe '他作家のフキダシ一覧取得に於いて' do
348 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
349 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
350 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
351 @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
352 @other_sb = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
355 r = SpeechBalloon.himlist @other_author
356 r.should eq [@other_sb]
359 new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
360 new_sb = FactoryGirl.create :speech_balloon, :panel_id => new_panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
361 r = SpeechBalloon.himlist @other_author
362 r.should eq [new_sb, @other_sb]
365 hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
366 hidden_sb = FactoryGirl.create :speech_balloon, :panel_id => hidden_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
367 r = SpeechBalloon.himlist @other_author
368 r.should eq [@other_sb]
370 context 'DBに5件あって1ページの件数を2件に変えたとして' do
372 @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
373 @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
374 @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
375 @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
378 r = SpeechBalloon.himlist @other_author, 1, 2
379 r.should have(2).items
381 it 'page=1なら末尾2件を返す' do
383 r = SpeechBalloon.himlist @other_author, 1, 2
384 r.should eq [@other_sb5, @other_sb4]
386 it 'page=2なら中間2件を返す' do
387 r = SpeechBalloon.himlist @other_author, 2, 2
388 r.should eq [@other_sb3, @other_sb2]
390 it 'page=3なら先頭1件を返す' do
391 r = SpeechBalloon.himlist @other_author, 3, 2
392 r.should eq [@other_sb]
397 describe 'フキダシ一覧ページ制御に於いて' do
399 SpeechBalloon.stub(:count).with(any_args).and_return(100)
402 r = SpeechBalloon.list_paginate
403 r.is_a?(Kaminari::PaginatableArray).should be_true
405 it 'フキダシ一覧の取得条件を利用している' do
406 SpeechBalloon.stub(:list_where).with(any_args).and_return('')
407 SpeechBalloon.should_receive(:list_where).with(any_args).exactly(1)
408 r = SpeechBalloon.list_paginate
410 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
411 r = SpeechBalloon.list_paginate 3, 10
412 r.limit_value.should eq 10
413 r.offset_value.should eq 20
417 describe '自分のフキダシ一覧ページ制御に於いて' do
419 SpeechBalloon.stub(:count).with(any_args).and_return(100)
422 r = SpeechBalloon.mylist_paginate @author
423 r.is_a?(Kaminari::PaginatableArray).should be_true
425 it '自分のフキダシ一覧の取得条件を利用している' do
426 SpeechBalloon.stub(:mylist_where).with(any_args).and_return('')
427 SpeechBalloon.should_receive(:mylist_where).with(any_args).exactly(1)
428 r = SpeechBalloon.mylist_paginate @author
430 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
431 r = SpeechBalloon.mylist_paginate @author, 3, 10
432 r.limit_value.should eq 10
433 r.offset_value.should eq 20
437 describe '他作家のフキダシ一覧ページ制御に於いて' do
439 SpeechBalloon.stub(:count).with(any_args).and_return(100)
442 r = SpeechBalloon.himlist_paginate @other_author
443 r.is_a?(Kaminari::PaginatableArray).should be_true
445 it '他作家のフキダシ一覧の取得条件を利用している' do
446 SpeechBalloon.stub(:himlist_where).with(any_args).and_return('')
447 SpeechBalloon.should_receive(:himlist_where).with(any_args).exactly(1)
448 r = SpeechBalloon.himlist_paginate @other_author
450 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
451 r = SpeechBalloon.himlist_paginate @other_author, 3, 10
452 r.limit_value.should eq 10
453 r.offset_value.should eq 20
457 describe '一覧取得オプションに於いて' do
459 r = SpeechBalloon.list_opt
460 r.should have(4).items
463 r = SpeechBalloon.list_opt
464 r.has_key?(:panel).should be_true
467 r = SpeechBalloon.list_opt
468 r[:panel].has_key?(:author).should be_true
471 r = SpeechBalloon.list_opt
472 r.has_key?(:balloon).should be_true
475 r = SpeechBalloon.list_opt
476 r.has_key?(:speech).should be_true
478 it 'フキダシテンプレートを含んでいる' do
479 r = SpeechBalloon.list_opt
480 r.has_key?(:speech_balloon_template).should be_true
483 describe 'json一覧出力オプションに於いて' do
485 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
486 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
487 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
488 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
491 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
494 i.has_key?('panel').should be_true
497 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
501 s.has_key?('author').should be_true
504 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
507 i.has_key?('balloon').should be_true
510 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
513 i.has_key?('speech').should be_true
515 it 'フキダシテンプレートを含んでいる' do
516 r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
519 i.has_key?('speech_balloon_template').should be_true
523 describe '単体取得に於いて' do
525 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
526 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
527 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
529 context 'つつがなく終わるとき' do
530 it '単体取得オプションを利用している' do
531 SpeechBalloon.stub(:show_opt).with(any_args).and_return({:include => :panel})
532 SpeechBalloon.should_receive(:show_opt).with(any_args).exactly(1)
533 r = SpeechBalloon.show @sb.id, @author
535 it '閲覧許可を問い合わせている' do
536 SpeechBalloon.any_instance.stub(:visible?).with(any_args).and_return(true)
537 SpeechBalloon.any_instance.should_receive(:visible?).with(any_args).exactly(1)
538 r = SpeechBalloon.show @sb.id, @author
542 SpeechBalloon.any_instance.stub(:visible?).and_return(true)
543 pl = SpeechBalloon.show @sb.id, @author
546 context '閲覧許可が出なかったとき' do
547 it '403Forbidden例外を返す' do
548 SpeechBalloon.any_instance.stub(:visible?).and_return(false)
550 SpeechBalloon.show @sb.id, @author
551 }.should raise_error(ActiveRecord::Forbidden)
554 context '存在しないフキダシを開こうとしたとき' do
555 it '404RecordNotFound例外を返す' do
557 SpeechBalloon.show 110, @author
558 }.should raise_error(ActiveRecord::RecordNotFound)
562 describe '単体取得オプションに於いて' do
563 it 'includeキーを含んでいる' do
564 r = SpeechBalloon.show_opt
565 r.has_key?(:include).should be_true
568 r = SpeechBalloon.show_opt[:include]
569 r.should have(4).items
572 r = SpeechBalloon.show_opt[:include]
573 r.has_key?(:panel).should be_true
576 r = SpeechBalloon.show_opt[:include]
577 r[:panel].has_key?(:author).should be_true
580 r = SpeechBalloon.show_opt[:include]
581 r.has_key?(:balloon).should be_true
584 r = SpeechBalloon.show_opt[:include]
585 r.has_key?(:speech).should be_true
587 it 'フキダシテンプレートを含んでいる' do
588 r = SpeechBalloon.show_opt[:include]
589 r.has_key?(:speech_balloon_template).should be_true
592 describe 'json単体出力オプションに於いて' do
594 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
595 @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
596 @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
597 @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
600 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
603 i.has_key?('panel').should be_true
606 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
610 s.has_key?('author').should be_true
613 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
616 i.has_key?('balloon').should be_true
619 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
622 i.has_key?('speech').should be_true
624 it 'フキダシテンプレートを含んでいる' do
625 r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
628 i.has_key?('speech_balloon_template').should be_true