OSDN Git Service

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