OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / panel_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コマ絵
4 describe PanelPicture do
5   before do
6     SpeechBalloonTemplate.delete_all
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
17     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
18     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
19     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
20     @panel = FactoryGirl.create :panel, :author_id => @author.id
21   end
22   
23   describe '検証に於いて' do
24     before do
25       @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
26         :width => @p.width, :height => @p.height
27       Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
28       Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
29       Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
30     end
31     
32     context 'オーソドックスなデータのとき' do
33       it '下限データが通る' do
34         @pp.link = 'http://test.jp/'
35         @pp.x = -99999
36         @pp.y = -99999
37         @pp.z = 1
38         @pp.t = 0
39         @pp.width = -99999
40         @pp.height = -99999
41         @pp.should be_valid
42       end
43       it '上限データが通る' do
44         @pp.link = 'http://test.jp/aaaaa' + 'a' * 180
45         @pp.x = 99999
46         @pp.y = 99999
47         @pp.z = 99999
48         @pp.t = 99999
49         @pp.width = 99999
50         @pp.height = 99999
51         @pp.should be_valid
52       end
53     end
54     
55     context 'panel_idを検証するとき' do
56       #ネストの保存はnilを許可しなければならないので数値チェックだけ
57       it '数値でなければ失敗する' do
58         @pp.panel_id = 'a'
59         @pp.should_not be_valid
60       end
61     end
62     context 'linkを検証するとき' do
63       it 'nullでも通る' do
64         @pp.link = ''
65         @pp.should be_valid
66       end
67       it '201文字以上なら失敗する' do
68         @pp.link = 'a'*201
69         @pp.should_not be_valid
70       end
71       it 'url形式でないなら失敗する' do
72         @pp.link = 'a'*200
73         @pp.should_not be_valid
74       end
75     end
76     context 'xを検証するとき' do
77       it 'nullなら失敗する' do
78         @pp.x = nil
79         @pp.should_not be_valid
80       end
81       it '数値でなければ失敗する' do
82         @pp.x = 'a'
83         @pp.should_not be_valid
84       end
85       it '0なら通る' do
86         @pp.x = '0'
87         @pp.should be_valid
88       end
89       it '負でも通る' do
90         @pp.x = -1
91         @pp.should be_valid
92       end
93     end
94     context 'yを検証するとき' do
95       it 'nullなら失敗する' do
96         @pp.y = nil
97         @pp.should_not be_valid
98       end
99       it '数値でなければ失敗する' do
100         @pp.y = 'a'
101         @pp.should_not be_valid
102       end
103       it '0なら通る' do
104         @pp.y = '0'
105         @pp.should be_valid
106       end
107       it '負でも通る' do
108         @pp.y = -1
109         @pp.should be_valid
110       end
111     end
112     context 'widthを検証するとき' do
113       it 'nullなら失敗する' do
114         @pp.width = nil
115         @pp.should_not be_valid
116       end
117       it '数値でなければ失敗する' do
118         @pp.width = 'a'
119         @pp.should_not be_valid
120       end
121       it '0なら失敗する' do
122         @pp.width = '0'
123         @pp.should_not be_valid
124       end
125       context  '反転許可のとき' do
126         before do
127         end
128         it '負でも通る' do
129           @pp.width = -1
130           @pp.should be_valid
131         end
132       end
133       context  '反転禁止のとき' do
134         before do
135           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
136         end
137         it '負なら失敗する' do
138           @pp.width = -1
139           @pp.should_not be_valid
140         end
141       end
142       context  'サイズ変更許可のとき' do
143         before do
144         end
145         it '実素材のサイズと違っても通る' do
146           @pp.width = @p.width-1
147           @pp.should be_valid
148         end
149       end
150       context  'サイズ変更禁止のとき' do
151         before do
152           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
153         end
154         it '実素材のサイズと違うなら失敗する' do
155           @pp.width = @p.width-1
156           @pp.should_not be_valid
157         end
158       end
159       context  '縦横比変更許可のとき' do
160         before do
161         end
162         it '実素材の縦横比と違っても通る' do
163           @pp.width = @p.width / 2
164           @pp.should be_valid
165         end
166       end
167       context  '縦横比変更禁止のとき' do
168         before do
169           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
170         end
171         it '実素材の縦横比と違うなら失敗する' do
172           @pp.width = @p.width / 2
173           @pp.should_not be_valid
174         end
175       end
176     end
177     context 'heightを検証するとき' do
178       it 'nullなら失敗する' do
179         @pp.height = nil
180         @pp.should_not be_valid
181       end
182       it '数値でなければ失敗する' do
183         @pp.height = 'a'
184         @pp.should_not be_valid
185       end
186       it '0なら失敗する' do
187         @pp.height = '0'
188         @pp.should_not be_valid
189       end
190       it '負でも通る' do
191         @pp.height = -1
192         @pp.should be_valid
193       end
194       context  '反転許可のとき' do
195         before do
196         end
197         it '負でも通る' do
198           @pp.height = -1
199           @pp.should be_valid
200         end
201       end
202       context  '反転禁止のとき' do
203         before do
204           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
205         end
206         it '負なら失敗する' do
207           @pp.height = -1
208           @pp.should_not be_valid
209         end
210       end
211       context  'サイズ変更許可のとき' do
212         before do
213         end
214         it '実素材のサイズと違っても通る' do
215           @pp.height = @p.height-1
216           @pp.should be_valid
217         end
218       end
219       context  'サイズ変更禁止のとき' do
220         before do
221           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
222         end
223         it '実素材のサイズと違うなら失敗する' do
224           @pp.height = @p.height-1
225           @pp.should_not be_valid
226         end
227       end
228       context  '縦横比変更許可のとき' do
229         before do
230         end
231         it '実素材の縦横比と違っても通る' do
232           @pp.height = @p.height / 2
233           @pp.should be_valid
234         end
235       end
236       context  '縦横比変更禁止のとき' do
237         before do
238           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
239         end
240         it '実素材の縦横比と違うなら失敗する' do
241           @pp.height = @p.height / 2
242           @pp.should_not be_valid
243         end
244       end
245     end
246     context 'zを検証するとき' do
247       it 'nullなら失敗する' do
248         @pp.z = nil
249         @pp.should_not be_valid
250       end
251       it '数値でなければ失敗する' do
252         @pp.z = 'a'
253         @pp.should_not be_valid
254       end
255       it '負なら失敗する' do
256         @pp.z = -1
257         @pp.should_not be_valid
258       end
259       it '負なら失敗する' do
260         @pp.z = 0
261         @pp.should_not be_valid
262       end
263     end
264     context 'tを検証するとき' do
265       it 'nullなら失敗する' do
266         @pp.t = nil
267         @pp.should_not be_valid
268       end
269       it '数値でなければ失敗する' do
270         @pp.t = 'a'
271         @pp.should_not be_valid
272       end
273       it '負なら失敗する' do
274         @pp.t = -1
275         @pp.should_not be_valid
276       end
277     end
278     context 'picture_idを検証するとき' do
279       it 'nullなら失敗する' do
280         @pp.picture_id = nil
281         @pp.should_not be_valid
282       end
283       it '数値でなければ失敗する' do
284         @pp.picture_id = 'a'
285         @pp.should_not be_valid
286       end
287       it '存在する実素材でなければ失敗する' do
288         @pp.picture_id = 0
289         @pp.should_not be_valid
290       end
291     end
292   end
293   
294   describe '文字コード検証に於いて' do
295     before do
296       @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
297         :width => @p.width, :height => @p.height
298     end
299     
300     context 'linkを検証するとき' do
301       it 'Shift JISなら失敗する' do
302         @pp.link = "\x83G\x83r\x83]\x83D"
303         lambda{
304           @pp.valid_encode
305         }.should raise_error(Pettanr::BadRequest)
306       end
307     end
308     
309     context 'captionを検証するとき' do
310       it 'Shift JISなら失敗する' do
311         @pp.caption = "\x83G\x83r\x83]\x83D"
312         lambda{
313           @pp.valid_encode
314         }.should raise_error(Pettanr::BadRequest)
315       end
316     end
317   end
318   
319   describe '閲覧許可に於いて' do
320     before do
321       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
322         :width => @p.width, :height => @p.height
323     end
324     context 'オープンモードのとき' do
325       before do
326         MagicNumber['run_mode'] = 0
327       end
328       it '自身にゲスト用ロールチェックを問い合わせしている' do
329         PanelPicture.any_instance.stub(:guest_role_check).and_return(true)
330         PanelPicture.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
331         r = @pp.visible?([@author])
332       end
333       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
334         PanelPicture.any_instance.stub(:guest_role_check).and_return(false)
335         r = @pp.visible?([@author])
336         r.should be_false
337       end
338     end
339     context 'クローズドモードのとき' do
340       before do
341         MagicNumber['run_mode'] = 1
342       end
343       it '自身に読者用ロールチェックを問い合わせしている' do
344         PanelPicture.any_instance.stub(:reader_role_check).and_return(true)
345         PanelPicture.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
346         r = @pp.visible?([@author])
347       end
348       it '読者用ロールチェックが失敗したとき、falseを返す' do
349         PanelPicture.any_instance.stub(:reader_role_check).and_return(false)
350         r = @pp.visible?([@author])
351         r.should be_false
352       end
353     end
354     context '事前チェックする' do
355       before do
356         MagicNumber['run_mode'] = 1
357         PanelPicture.any_instance.stub(:reader_role_check).and_return(true)
358       end
359       it '自身のコマに所持判定を問い合わせしている' do
360         Panel.any_instance.stub(:own?).and_return(true)
361         Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
362         r = @pp.visible?([@author])
363       end
364       it '自身のコマに閲覧許可を問い合わせしている' do
365         Panel.any_instance.stub(:own?).and_return(false)
366         Panel.any_instance.stub(:visible?).and_return(true)
367         Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
368         r = @pp.visible?([@author])
369       end
370     end
371     context 'つつがなく終わるとき' do
372       before do
373         MagicNumber['run_mode'] = 1
374         Panel.any_instance.stub(:reader_role_check).and_return(true)
375       end
376       it '自分のコマのコマ絵なら許可する' do
377         Panel.any_instance.stub(:own?).and_return(true)
378         Panel.any_instance.stub(:visible?).and_return(false)
379         r = @pp.visible?([@author])
380         r.should be_true
381       end
382       it '他人の非公開コマのコマ絵なら許可しない' do
383         Panel.any_instance.stub(:own?).and_return(false)
384         Panel.any_instance.stub(:visible?).and_return(false)
385         r = @pp.visible?([@author])
386         r.should be_false
387       end
388       it '他人のコマのコマ絵でも公開なら許可する' do
389         Panel.any_instance.stub(:own?).and_return(false)
390         Panel.any_instance.stub(:visible?).and_return(true)
391         r = @pp.visible?([@author])
392         r.should be_true
393       end
394     end
395   end
396   
397   describe '一覧取得に於いて' do
398     before do
399       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
400         :width => @p.width, :height => @p.height
401     end
402     context 'page補正について' do
403       it '文字列から数値に変換される' do
404         PanelPicture.page('8').should eq 8
405       end
406       it 'nilの場合は1になる' do
407         PanelPicture.page().should eq 1
408       end
409       it '0以下の場合は1になる' do
410         PanelPicture.page('0').should eq 1
411       end
412     end
413     context 'page_size補正について' do
414       it '文字列から数値に変換される' do
415         PanelPicture.page_size('7').should eq 7
416       end
417       it 'nilの場合はPanelPicture.default_page_sizeになる' do
418         PanelPicture.page_size().should eq PanelPicture.default_page_size
419       end
420       it '0以下の場合はPanelPicture.default_page_sizeになる' do
421         PanelPicture.page_size('0').should eq PanelPicture.default_page_size
422       end
423       it 'PanelPicture.max_page_sizeを超えた場合はPanelPicture.max_page_sizeになる' do
424         PanelPicture.page_size('1000').should eq PanelPicture.max_page_size
425       end
426     end
427     context 'つつがなく終わるとき' do
428       it '一覧取得オプションを利用している' do
429         PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
430         PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)
431         r = PanelPicture.list
432       end
433     end
434     it 'リストを返す' do
435       r = PanelPicture.list
436       r.should eq [@pp]
437     end
438     it '時系列で並んでいる' do
439       #公開されたコマのコマ絵は(他人のコマ絵であっても)含んでいる
440       hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
441       npl = FactoryGirl.create :panel_picture, :panel_id => hc.id, :t => 0, :picture_id => @p.id,
442         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
443       r = PanelPicture.list
444       r.should eq [npl, @pp]
445     end
446     it '非公開のコマのコマ絵は自分のコマ絵であっても含まない' do
447       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
448       npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
449         :width => @p.width, :height => @p.height
450       pl = PanelPicture.list
451       pl.should eq [@pp]
452     end
453     context 'DBに5件あって1ページの件数を2件に変えたとして' do
454       before do
455         @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
456           :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
457         @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
458           :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
459         @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
460           :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
461         @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
462           :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
463         PanelPicture.stub(:default_page_size).and_return(2)
464       end
465       it '通常は2件を返す' do
466         pl = PanelPicture.list
467         pl.should have(2).items 
468       end
469       it 'page=1なら末尾2件を返す' do
470         #時系列で並んでいる
471         pl = PanelPicture.list(1)
472         pl.should eq [@npl5, @npl4]
473       end
474       it 'page=2なら中間2件を返す' do
475         pl = PanelPicture.list(2)
476         pl.should eq [@npl3, @npl2]
477       end
478       it 'page=3なら先頭1件を返す' do
479         pl = PanelPicture.list(3)
480         pl.should eq [@pp]
481       end
482     end
483   end
484   
485   describe '自分のコマ絵一覧取得に於いて' do
486     before do
487       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
488         :width => @p.width, :height => @p.height
489     end
490     context 'つつがなく終わるとき' do
491       it '一覧取得オプションを利用している' do
492         PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
493         PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)
494         r = PanelPicture.mylist @author
495       end
496     end
497     it 'リストを返す' do
498       pl = PanelPicture.mylist @author
499       pl.should eq [@pp]
500     end
501     it '時系列で並んでいる' do
502       npl = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
503         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
504       pl = PanelPicture.mylist @author
505       pl.should eq [npl, @pp]
506     end
507     it '他人のコマのコマ絵は公開コマでも含まない' do
508       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
509       npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
510         :width => @p.width, :height => @p.height
511       pl = PanelPicture.mylist @author
512       pl.should eq [@pp]
513     end
514     it '自分のコマのコマ絵は非公開コマでも含んでいる' do
515       pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
516       ni = FactoryGirl.create :panel_picture, :panel_id => pl.id, :picture_id => @p.id,
517         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
518       r = PanelPicture.mylist @author
519       r.should eq [ni, @pp]
520     end
521     context 'DBに5件あって1ページの件数を2件に変えたとして' do
522       before do
523         @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
524           :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
525         @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
526           :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
527         @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
528           :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
529         @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
530           :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
531       end
532       it '通常は2件を返す' do
533         r = PanelPicture.mylist @author, 1, 2
534         r.should have(2).items 
535       end
536       it 'page=1なら末尾2件を返す' do
537         #時系列で並んでいる
538         r = PanelPicture.mylist(@author, 1, 2)
539         r.should eq [@npl5, @npl4]
540       end
541       it 'page=2なら中間2件を返す' do
542         r = PanelPicture.mylist(@author, 2, 2)
543         r.should eq [@npl3, @npl2]
544       end
545       it 'page=3なら先頭1件を返す' do
546         r = PanelPicture.mylist(@author, 3, 2)
547         r.should eq [@pp]
548       end
549     end
550   end
551   
552   describe '他作家のコマ絵一覧取得に於いて' do
553     before do
554       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
555         :width => @p.width, :height => @p.height
556       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
557       @other_pp = FactoryGirl.create :panel_picture, :panel_id => @other_panel.id, :picture_id => @p.id,
558         :width => @p.width, :height => @p.height
559     end
560     it 'リストを返す' do
561       r = PanelPicture.himlist @other_author
562       r.should eq [@other_pp]
563     end
564     it '時系列で並んでいる' do
565       new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
566       new_pc = FactoryGirl.create :panel_picture, :panel_id => new_panel.id, :picture_id => @p.id, :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
567       r = PanelPicture.himlist @other_author
568       r.should eq [new_pc, @other_pp]
569     end
570     it '公開コマに限る' do
571       hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
572       hidden_pp = FactoryGirl.create :panel_picture, :panel_id => hidden_panel.id, :picture_id => @p.id, :width => @p.width, :height => @p.height
573       r = PanelPicture.himlist @other_author
574       r.should eq [@other_pp]
575     end
576     context 'DBに5件あって1ページの件数を2件に変えたとして' do
577       before do
578         @other_pp2 = FactoryGirl.create :panel_picture, :panel_id => @other_panel.id, :t => 1, :picture_id => @p.id, :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
579         @other_pp3 = FactoryGirl.create :panel_picture, :panel_id => @other_panel.id, :t => 2, :picture_id => @p.id, :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
580         @other_pp4 = FactoryGirl.create :panel_picture, :panel_id => @other_panel.id, :t => 3, :picture_id => @p.id, :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
581         @other_pp5 = FactoryGirl.create :panel_picture, :panel_id => @other_panel.id, :t => 4, :picture_id => @p.id, :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
582       end
583       it '通常は2件を返す' do
584         pl = PanelPicture.himlist @other_author, 1, 2
585         pl.should have(2).items 
586       end
587       it 'page=1なら末尾2件を返す' do
588         #時系列で並んでいる
589         pl = PanelPicture.himlist @other_author, 1, 2
590         pl.should eq [@other_pp5, @other_pp4]
591       end
592       it 'page=2なら中間2件を返す' do
593         pl = PanelPicture.himlist @other_author, 2, 2
594         pl.should eq [@other_pp3, @other_pp2]
595       end
596       it 'page=3なら先頭1件を返す' do
597         pl = PanelPicture.himlist @other_author, 3, 2
598         pl.should eq [@other_pp]
599       end
600     end
601   end
602   
603   describe 'コマ絵一覧ページ制御に於いて' do
604     before do
605       PanelPicture.stub(:count).with(any_args).and_return(100)
606     end
607     it 'ページ制御を返す' do
608       r = PanelPicture.list_paginate 
609       r.is_a?(Kaminari::PaginatableArray).should be_true
610     end
611     it 'コマ絵一覧の取得条件を利用している' do
612       PanelPicture.stub(:list_where).with(any_args).and_return('')
613       PanelPicture.should_receive(:list_where).with(any_args).exactly(1)
614       r = PanelPicture.list_paginate 
615     end
616     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
617       r = PanelPicture.list_paginate 3, 10
618       r.limit_value.should eq 10
619       r.offset_value.should eq 20
620     end
621   end
622   
623   describe '自分のコマ絵一覧ページ制御に於いて' do
624     before do
625       PanelPicture.stub(:count).with(any_args).and_return(100)
626     end
627     it 'ページ制御を返す' do
628       r = PanelPicture.mylist_paginate @author
629       r.is_a?(Kaminari::PaginatableArray).should be_true
630     end
631     it '自分のコマ絵一覧の取得条件を利用している' do
632       PanelPicture.stub(:mylist_where).with(any_args).and_return('')
633       PanelPicture.should_receive(:mylist_where).with(any_args).exactly(1)
634       r = PanelPicture.mylist_paginate @author
635     end
636     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
637       r = PanelPicture.mylist_paginate @author, 3, 10
638       r.limit_value.should eq 10
639       r.offset_value.should eq 20
640     end
641   end
642   
643   describe '他作家のコマ絵一覧ページ制御に於いて' do
644     before do
645       PanelPicture.stub(:count).with(any_args).and_return(100)
646     end
647     it 'ページ制御を返す' do
648       r = PanelPicture.himlist_paginate @other_author
649       r.is_a?(Kaminari::PaginatableArray).should be_true
650     end
651     it '他作家のコマ絵一覧の取得条件を利用している' do
652       PanelPicture.stub(:himlist_where).with(any_args).and_return('')
653       PanelPicture.should_receive(:himlist_where).with(any_args).exactly(1)
654       r = PanelPicture.himlist_paginate @other_author
655     end
656     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
657       r = PanelPicture.himlist_paginate @other_author, 3, 10
658       r.limit_value.should eq 10
659       r.offset_value.should eq 20
660     end
661   end
662   
663   describe '一覧取得オプションに於いて' do
664     it '2つの項目を含んでいる' do
665       r = PanelPicture.list_opt
666       r.should have(2).items
667     end
668     it 'コマを含んでいる' do
669       r = PanelPicture.list_opt
670       r.has_key?(:panel).should be_true
671     end
672       it 'コマは作家を含んでいる' do
673         r = PanelPicture.list_opt
674         r[:panel].has_key?(:author).should be_true
675       end
676     it '実素材を含んでいる' do
677       r = PanelPicture.list_opt
678       r.has_key?(:picture).should be_true
679     end
680       it '実素材は絵師を含んでいる' do
681         r = PanelPicture.list_opt
682         r[:picture].has_key?(:artist).should be_true
683       end
684       it '実素材はライセンスを含んでいる' do
685         r = PanelPicture.list_opt
686         r[:picture].has_key?(:license).should be_true
687       end
688   end
689   describe 'json一覧出力オプションに於いて' do
690     before do
691       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
692       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
693       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
694       @sbt = FactoryGirl.create :speech_balloon_template
695       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
696       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
697         :width => @p.width, :height => @p.height
698     end
699     it 'コマを含んでいる' do
700       r = PanelPicture.list.to_json PanelPicture.list_json_opt
701       j = JSON.parse r
702       i = j.first
703       i.has_key?('panel').should be_true
704     end
705       it 'コマは作家を含んでいる' do
706         r = PanelPicture.list.to_json PanelPicture.list_json_opt
707         j = JSON.parse r
708         i = j.first
709         s = i['panel']
710         s.has_key?('author').should be_true
711       end
712     it '実素材を含んでいる' do
713       r = PanelPicture.list.to_json PanelPicture.list_json_opt
714       j = JSON.parse r
715       i = j.first
716       i.has_key?('picture').should be_true
717     end
718       it '実素材は絵師を含んでいる' do
719         r = PanelPicture.list.to_json PanelPicture.list_json_opt
720         j = JSON.parse r
721         i = j.first
722         s = i['picture']
723         s.has_key?('artist').should be_true
724       end
725       it '実素材はライセンスを含んでいる' do
726         r = PanelPicture.list.to_json PanelPicture.list_json_opt
727         j = JSON.parse r
728         i = j.first
729         s = i['picture']
730         s.has_key?('license').should be_true
731       end
732   end
733   
734   describe '単体取得に於いて' do
735     before do
736       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
737         :width => @p.width, :height => @p.height
738     end
739     context 'つつがなく終わるとき' do
740       it '単体取得オプションを利用している' do
741         PanelPicture.stub(:show_opt).with(any_args).and_return({:include => :panel})
742         PanelPicture.should_receive(:show_opt).with(any_args).exactly(1)
743         r = PanelPicture.show @pp.id, @author
744       end
745       it '閲覧許可を問い合わせている' do
746         PanelPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
747         PanelPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
748         r = PanelPicture.show @pp.id, @author
749       end
750     end
751     it '指定のコマ絵を返す' do
752       PanelPicture.any_instance.stub(:visible?).and_return(true)
753       pl = PanelPicture.show @pp.id, @author
754       pl.should eq @pp
755     end
756     context '閲覧許可が出なかったとき' do
757       it '403Forbidden例外を返す' do
758         PanelPicture.any_instance.stub(:visible?).and_return(false)
759         lambda{
760           PanelPicture.show @pp.id, @author
761         }.should raise_error(ActiveRecord::Forbidden)
762       end
763     end
764     context '存在しないコマ絵を開こうとしたとき' do
765       it '404RecordNotFound例外を返す' do
766         lambda{
767           PanelPicture.show 110, @author
768         }.should raise_error(ActiveRecord::RecordNotFound)
769       end
770     end
771   end
772   describe '単体取得オプションに於いて' do
773     it 'includeキーを含んでいる' do
774       r = PanelPicture.show_opt
775       r.has_key?(:include).should be_true
776     end
777     it '2つの項目を含んでいる' do
778       r = PanelPicture.show_opt[:include]
779       r.should have(2).items
780     end
781     it 'コマを含んでいる' do
782       r = PanelPicture.show_opt[:include]
783       r.has_key?(:panel).should be_true
784     end
785       it 'コマは作家を含んでいる' do
786         r = PanelPicture.show_opt[:include]
787         r[:panel].has_key?(:author).should be_true
788       end
789     it '実素材を含んでいる' do
790       r = PanelPicture.show_opt[:include]
791       r.has_key?(:picture).should be_true
792     end
793       it '実素材は絵師を含んでいる' do
794         r = PanelPicture.show_opt[:include]
795         r[:picture].has_key?(:artist).should be_true
796       end
797       it '実素材はライセンスを含んでいる' do
798         r = PanelPicture.show_opt[:include]
799         r[:picture].has_key?(:license).should be_true
800       end
801   end
802   describe 'json単体出力オプションに於いて' do
803     before do
804       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
805       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
806       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
807       @sbt = FactoryGirl.create :speech_balloon_template
808       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
809       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
810         :width => @p.width, :height => @p.height
811     end
812     it 'コマを含んでいる' do
813       r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
814       j = JSON.parse r
815       i = j
816       i.has_key?('panel').should be_true
817     end
818       it 'コマは作家を含んでいる' do
819         r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
820         j = JSON.parse r
821         i = j
822         s = i['panel']
823         s.has_key?('author').should be_true
824       end
825     it '実素材を含んでいる' do
826       r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
827       j = JSON.parse r
828       i = j
829       i.has_key?('picture').should be_true
830     end
831       it '実素材は絵師を含んでいる' do
832         r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
833         j = JSON.parse r
834         i = j
835         s = i['picture']
836         s.has_key?('artist').should be_true
837       end
838       it '実素材はライセンスを含んでいる' do
839         r = PanelPicture.show(@pp.id, @author).to_json PanelPicture.show_json_opt
840         j = JSON.parse r
841         i = j
842         s = i['picture']
843         s.has_key?('license').should be_true
844       end
845   end
846 end