OSDN Git Service

t#32246:add sheet
[pettanr/pettanr.git] / spec / models / sheet_panel_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #紙コマ
4 describe SheetPanel do
5   before do
6     SpeechBalloonTemplate.delete_all
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     SpeechBalloonTemplate.delete_all
12     @speech_balloon_template = FactoryGirl.create(:speech_balloon_template, "name" => "circle@pettan.com", "classname" => "CircleSpeechBalloon", "caption" => "cc",  "system_picture_id" => @sp.id, "settings" => '{}')
13     @writing_format = FactoryGirl.create :writing_format
14     @user = FactoryGirl.create( :user_yas)
15     @author = FactoryGirl.create :author, :user_id => @user.id
16     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
17     @other_user = FactoryGirl.create( :user_yas)
18     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
19     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
20     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
21     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
22   end
23   
24   describe '検証に於いて' do
25     before do
26       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
27       @panel = FactoryGirl.create :panel, :author_id => @author.id
28       @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
29     end
30     
31     context 'オーソドックスなデータのとき' do
32       it '下限データが通る' do
33         @sheet_panel.x = -99999
34         @sheet_panel.y = -99999
35         @sheet_panel.z = 1
36         @sheet_panel.t = 0
37         @sheet_panel.should be_valid
38       end
39       it '上限データが通る' do
40         @sheet_panel.x = 99999
41         @sheet_panel.y = 99999
42         @sheet_panel.z = 99999
43         @sheet_panel.t = 99999
44         @sheet_panel.should be_valid
45       end
46     end
47     
48     context 'sheet_idを検証するとき' do
49       it 'nullなら失敗する' do
50         @sheet_panel.sheet_id = nil
51         @sheet_panel.should_not be_valid
52       end
53       it '数値でなければ失敗する' do
54         @sheet_panel.sheet_id = 'a'
55         @sheet_panel.should_not be_valid
56       end
57       it '存在する用紙でなければ失敗する' do
58         @sheet_panel.sheet_id = 0
59         @sheet_panel.should_not be_valid
60       end
61     end
62     
63     context 'panel_idを検証するとき' do
64       it 'nullなら失敗する' do
65         @sheet_panel.panel_id = nil
66         @sheet_panel.should_not be_valid
67       end
68       it '数値でなければ失敗する' do
69         @sheet_panel.panel_id = 'a'
70         @sheet_panel.should_not be_valid
71       end
72       it '存在するコマでなければ失敗する' do
73         @sheet_panel.panel_id = 0
74         @sheet_panel.should_not be_valid
75       end
76     end
77     
78     context 'xを検証するとき' do
79       it 'nullなら失敗する' do
80         @sheet_panel.x = nil
81         @sheet_panel.should_not be_valid
82       end
83       it '数値でなければ失敗する' do
84         @sheet_panel.x = 'a'
85         @sheet_panel.should_not be_valid
86       end
87       it '0なら通る' do
88         @sheet_panel.x = '0'
89         @sheet_panel.should be_valid
90       end
91       it '負でも通る' do
92         @sheet_panel.x = -1
93         @sheet_panel.should be_valid
94       end
95     end
96     context 'yを検証するとき' do
97       it 'nullなら失敗する' do
98         @sheet_panel.y = nil
99         @sheet_panel.should_not be_valid
100       end
101       it '数値でなければ失敗する' do
102         @sheet_panel.y = 'a'
103         @sheet_panel.should_not be_valid
104       end
105       it '0なら通る' do
106         @sheet_panel.y = '0'
107         @sheet_panel.should be_valid
108       end
109       it '負でも通る' do
110         @sheet_panel.y = -1
111         @sheet_panel.should be_valid
112       end
113     end
114     context 'zを検証するとき' do
115       it 'nullなら失敗する' do
116         @sheet_panel.z = nil
117         @sheet_panel.should_not be_valid
118       end
119       it '数値でなければ失敗する' do
120         @sheet_panel.z = 'a'
121         @sheet_panel.should_not be_valid
122       end
123       it '負なら失敗する' do
124         @sheet_panel.z = -1
125         @sheet_panel.should_not be_valid
126       end
127       it '0なら失敗する' do
128         @sheet_panel.z = 0
129         @sheet_panel.should_not be_valid
130       end
131     end
132     context 'tを検証するとき' do
133       it 'nullなら失敗する' do
134         @sheet_panel.t = nil
135         @sheet_panel.should_not be_valid
136       end
137       it '数値でなければ失敗する' do
138         @sheet_panel.t = 'a'
139         @sheet_panel.should_not be_valid
140       end
141       it '負なら失敗する' do
142         @sheet_panel.t = -1
143         @sheet_panel.should_not be_valid
144       end
145     end
146     
147     context 'author_idを検証するとき' do
148       it 'nullなら失敗する' do
149         @sheet_panel.author_id = nil
150         @sheet_panel.should_not be_valid
151       end
152       it '数値でなければ失敗する' do
153         @sheet_panel.author_id = 'a'
154         @sheet_panel.should_not be_valid
155       end
156       it '存在する作家でなければ失敗する' do
157         @sheet_panel.author_id = 0
158         @sheet_panel.should_not be_valid
159       end
160     end
161     context '全体を検証するとき' do
162     end
163   end
164   
165   describe 'デフォルト値補充に於いて' do
166     before do
167       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
168       @panel = FactoryGirl.create :panel, :author_id => @author.id
169     end
170     
171     #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
172     it 'tをnilにする' do
173       @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
174       @sheet_panel.supply_default
175       @sheet_panel.t.should be_nil
176     end
177     
178   end
179   
180   describe '上書き補充に於いて' do
181     before do
182       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
183       @panel = FactoryGirl.create :panel, :author_id => @author.id
184     end
185     
186     context 'author_idを補充' do
187       it '問答無用でauthor_idを補充する' do
188         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
189         @sheet_panel.author_id = nil
190         @sheet_panel.overwrite @author
191         @sheet_panel.author_id.should eq @author.id
192       end
193     end
194     
195   end
196   
197   describe '所持判定に於いて' do
198     before do
199       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
200       @panel = FactoryGirl.create :panel, :author_id => @author.id
201       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
202       @sheeto = FactoryGirl.create :sheet, :author_id => @other_author.id
203       @panelo = FactoryGirl.create :panel, :author_id => @other_author.id
204       @sheet_panelo = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @sheeto.id, :panel_id => @panelo.id
205     end
206     context '事前チェックする' do
207       it '自身にロールリストからの作家取得を依頼している' do
208         SheetPanel.should_receive(:get_author_from_roles).with(any_args).exactly(1)
209         r = @sheet_panel.own?([@author])
210       end
211     end
212     context 'ロール内作家が取得できるとき' do
213       before do
214       end
215       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
216         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@author)
217         r = @sheet_panel.own?([@author])
218         r.should be_true
219       end
220       it 'ロール内作家のidが自身の作家idと一致しないならno' do
221         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
222         @sheet_panel.own?(@other_author).should be_false
223       end
224     end
225     context 'ロール内作家が取得できないとき' do
226       before do
227         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(nil)
228       end
229       it 'Falseを返す' do
230         r = @sheet_panel.own?([@author])
231         r.should be_false
232       end
233     end
234   end
235   
236   describe '閲覧許可に於いて' do
237     before do
238       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
239       @panel = FactoryGirl.create :panel, :author_id => @author.id
240       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
241     end
242     context 'オープンモードのとき' do
243       before do
244         MagicNumber['run_mode'] = 0
245       end
246       it '自身にゲスト用ロールチェックを問い合わせしている' do
247         SheetPanel.any_instance.stub(:guest_role_check).and_return(true)
248         SheetPanel.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
249         r = @sheet_panel.visible?([@author])
250       end
251       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
252         SheetPanel.any_instance.stub(:guest_role_check).and_return(false)
253         r = @sheet_panel.visible?([@author])
254         r.should be_false
255       end
256     end
257     context 'クローズドモードのとき' do
258       before do
259         MagicNumber['run_mode'] = 1
260       end
261       it '自身に読者用ロールチェックを問い合わせしている' do
262         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
263         SheetPanel.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
264         r = @sheet_panel.visible?([@author])
265       end
266       it '読者用ロールチェックが失敗したとき、falseを返す' do
267         SheetPanel.any_instance.stub(:reader_role_check).and_return(false)
268         r = @sheet_panel.visible?([@author])
269         r.should be_false
270       end
271     end
272     context '事前チェックする' do
273       before do
274         MagicNumber['run_mode'] = 1
275         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
276       end
277       it '自身の用紙に所持判定を問い合わせしている' do
278         Sheet.any_instance.stub(:own?).and_return(true)
279         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
280         r = @sheet_panel.visible?([@author])
281       end
282       it '自身の用紙に閲覧許可を問い合わせしている' do
283         Sheet.any_instance.stub(:own?).and_return(false)
284         Sheet.any_instance.stub(:visible?).and_return(true)
285         Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
286         r = @sheet_panel.visible?([@author])
287       end
288     end
289     context 'つつがなく終わるとき' do
290       before do
291         MagicNumber['run_mode'] = 1
292         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
293       end
294       it '自分の用紙の紙コマなら許可する' do
295         Sheet.any_instance.stub(:own?).and_return(true)
296         Sheet.any_instance.stub(:visible).and_return(0)
297         r = @sheet_panel.visible?([@author])
298         r.should be_true
299       end
300       it '他人の非公開用紙の紙コマなら許可しない' do
301         Sheet.any_instance.stub(:own?).and_return(false)
302         Sheet.any_instance.stub(:visible).and_return(0)
303         r = @sheet_panel.visible?([@author])
304         r.should be_false
305       end
306       it '他人の用紙の紙コマでも公開なら許可する' do
307         Sheet.any_instance.stub(:own?).and_return(false)
308         Sheet.any_instance.stub(:visible).and_return(1)
309         r = @sheet_panel.visible?([@author])
310         r.should be_true
311       end
312     end
313   end
314   
315   describe 'プレイリスト取得に於いて' do
316     before do
317       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
318       @panel = FactoryGirl.create :panel, :author_id => @author.id
319       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
320       @panel2 = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
321       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
322       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
323     end
324     it 'リストを返す' do
325       c = SheetPanel.play_list @sheet, @author
326       c.should eq [@sheet_panel]
327     end
328     it 't順で並んでいる' do
329       #公開用紙の公開コマは(他人の用紙であっても)含んでいる
330       v = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
331       c = SheetPanel.play_list @sheet, @author
332       c.should eq [ @sheet_panel, v]
333     end
334     it '非公開のコマは含んでいる' do
335       h = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel2.id, :t => 1
336       c = SheetPanel.play_list @sheet, @author
337       c.should eq [ @sheet_panel, h]
338     end
339   end
340   
341   describe '一覧取得に於いて' do
342     before do
343       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
344       @panel = FactoryGirl.create :panel, :author_id => @author.id
345       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
346       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
347       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
348       @hidden_sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
349     end
350     context 'page補正について' do
351       it '文字列から数値に変換される' do
352         SheetPanel.page('8').should eq 8
353       end
354       it 'nilの場合は1になる' do
355         SheetPanel.page().should eq 1
356       end
357       it '0以下の場合は1になる' do
358         SheetPanel.page('0').should eq 1
359       end
360     end
361     context 'page_size補正について' do
362       it '文字列から数値に変換される' do
363         SheetPanel.page_size('7').should eq 7
364       end
365       it 'nilの場合はSheetPanel.default_page_sizeになる' do
366         SheetPanel.page_size().should eq SheetPanel.default_page_size
367       end
368       it '0以下の場合はSheetPanel.default_page_sizeになる' do
369         SheetPanel.page_size('0').should eq SheetPanel.default_page_size
370       end
371       it 'SheetPanel.max_page_sizeを超えた場合はSheetPanel.max_page_sizeになる' do
372         SheetPanel.page_size('1000').should eq SheetPanel.max_page_size
373       end
374     end
375     it 'リストを返す' do
376       c = SheetPanel.list
377       c.should eq [@sheet_panel]
378     end
379     it '時系列で並んでいる' do
380       #公開用紙のSheetPanelは(他人のSheetPanelであっても)含んでいる
381       v = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 0, :updated_at => Time.now + 100
382       c = SheetPanel.list 
383       c.should eq [ v, @sheet_panel]
384     end
385     it '非公開の紙コマは(自分の紙コマであっても)含まない' do
386       h = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @hidden_sheet.id, :panel_id => @panel.id, :t => 0
387       c = SheetPanel.list 
388       c.should eq [ @sheet_panel]
389     end
390     context 'DBに5件あって1ページの件数を2件に変えたとして' do
391       before do
392         @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
393         @sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
394         @sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
395         @sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
396       end
397       it '通常は2件を返す' do
398         l = SheetPanel.list 1, 2
399         l.should have(2).items 
400       end
401       it 'page=1なら末尾2件を返す' do
402         #時系列で並んでいる
403         l = SheetPanel.list 1, 2
404         l.should eq [@sheet_panel5, @sheet_panel4]
405       end
406       it 'page=2なら中間2件を返す' do
407         l = SheetPanel.list 2, 2
408         l.should eq [@sheet_panel3, @sheet_panel2]
409       end
410       it 'page=3なら先頭1件を返す' do
411         l = SheetPanel.list 3, 2
412         l.should eq [@sheet_panel]
413       end
414     end
415   end
416   
417   describe '自分の紙コマ一覧取得に於いて' do
418     before do
419       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
420       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
421       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
422       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
423       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
424       @hsheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
425       @hpanel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
426     end
427     context 'つつがなく終わるとき' do
428       it '一覧取得オプションを利用している' do
429         SheetPanel.stub(:list_opt).with(any_args).and_return({})
430         SheetPanel.should_receive(:list_opt).with(any_args).exactly(1)
431         r = SheetPanel.mylist @author
432       end
433     end
434     it 'リストを返す' do
435       s = SheetPanel.mylist @author
436       s.should eq [@sheet_panel]
437     end
438     it '時系列で並んでいる' do
439       ns = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
440       sl = SheetPanel.mylist @author
441       sl.should eq [ns, @sheet_panel]
442     end
443     it '他人の紙コマはコマ用紙ともに公開でも含まない' do
444       so = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id
445       sl = SheetPanel.mylist @author
446       sl.should eq [@sheet_panel]
447     end
448     it '自分の紙コマはコマ用紙ともに非公開でも含んでいる' do
449       hs = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @hsheet.id, :panel_id => @hpanel.id, :updated_at => Time.now + 100
450       sl = SheetPanel.mylist @author
451       sl.should eq [hs, @sheet_panel]
452     end
453     context 'DBに5件あって1ページの件数を2件に変えたとして' do
454       before do
455         @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
456         @sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
457         @sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
458         @sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
459         SheetPanel.stub(:default_page_size).and_return(2)
460       end
461       it '通常は2件を返す' do
462         l = SheetPanel.mylist @author, 1, 2
463         l.should have(2).items 
464       end
465       it 'page=1なら末尾2件を返す' do
466         #時系列で並んでいる
467         l = SheetPanel.mylist @author, 1, 2
468         l.should eq [@sheet_panel5, @sheet_panel4]
469       end
470       it 'page=2なら中間2件を返す' do
471         l = SheetPanel.mylist @author, 2, 2
472         l.should eq [@sheet_panel3, @sheet_panel2]
473       end
474       it 'page=3なら先頭1件を返す' do
475         l = SheetPanel.mylist @author, 3, 2
476         l.should eq [@sheet_panel]
477       end
478     end
479   end
480   
481   describe '他作家の紙コマ一覧取得に於いて' do
482     before do
483       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
484       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
485       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
486       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
487       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
488       @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id
489     end
490     it 'リストを返す' do
491       r = SheetPanel.himlist @other_author
492       r.should eq [@other_sheet_panel]
493     end
494     it '時系列で並んでいる' do
495       ns = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
496       r = SheetPanel.himlist @other_author
497       r.should eq [ns, @other_sheet_panel]
498     end
499     it '公開用紙の紙コマ' do
500       @hsheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
501       ns = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @hsheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
502       r = SheetPanel.himlist @other_author
503       r.should eq [@other_sheet_panel]
504     end
505     context 'DBに5件あって1ページの件数を2件に変えたとして' do
506       before do
507         @other_sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
508         @other_sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 2, :updated_at => Time.now + 200
509         @other_sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 3, :updated_at => Time.now + 300
510         @other_sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 4, :updated_at => Time.now + 400
511         SheetPanel.stub(:default_page_size).and_return(2)
512       end
513       it '通常は2件を返す' do
514         l = SheetPanel.himlist @other_author, 1, 2
515         l.should have(2).items 
516       end
517       it 'page=1なら末尾2件を返す' do
518         #時系列で並んでいる
519         l = SheetPanel.himlist @other_author, 1, 2
520         l.should eq [@other_sheet_panel5, @other_sheet_panel4]
521       end
522       it 'page=2なら中間2件を返す' do
523         l = SheetPanel.himlist @other_author, 2, 2
524         l.should eq [@other_sheet_panel3, @other_sheet_panel2]
525       end
526       it 'page=3なら先頭1件を返す' do
527         l = SheetPanel.himlist @other_author, 3, 2
528         l.should eq [@other_sheet_panel]
529       end
530     end
531   end
532   
533   describe '紙コマ一覧ページ制御に於いて' do
534     before do
535       SheetPanel.stub(:count).with(any_args).and_return(100)
536     end
537     it 'ページ制御を返す' do
538       r = SheetPanel.list_paginate 
539       r.is_a?(Kaminari::PaginatableArray).should be_true
540     end
541     it '紙コマ一覧の取得条件を利用している' do
542       SheetPanel.stub(:list_where).with(any_args).and_return('')
543       SheetPanel.should_receive(:list_where).with(any_args).exactly(1)
544       r = SheetPanel.list_paginate 
545     end
546     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
547       r = SheetPanel.list_paginate 3, 10
548       r.limit_value.should eq 10
549       r.offset_value.should eq 20
550     end
551   end
552   
553   describe '自分の紙コマ一覧ページ制御に於いて' do
554     before do
555       SheetPanel.stub(:count).with(any_args).and_return(100)
556     end
557     it 'ページ制御を返す' do
558       r = SheetPanel.mylist_paginate @author
559       r.is_a?(Kaminari::PaginatableArray).should be_true
560     end
561     it '自分の紙コマ一覧の取得条件を利用している' do
562       SheetPanel.stub(:mylist_where).with(any_args).and_return('')
563       SheetPanel.should_receive(:mylist_where).with(any_args).exactly(1)
564       r = SheetPanel.mylist_paginate @author
565     end
566     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
567       r = SheetPanel.mylist_paginate @author, 3, 10
568       r.limit_value.should eq 10
569       r.offset_value.should eq 20
570     end
571   end
572   
573   describe '他作家の紙コマ一覧ページ制御に於いて' do
574     before do
575       SheetPanel.stub(:count).with(any_args).and_return(100)
576     end
577     it 'ページ制御を返す' do
578       r = SheetPanel.himlist_paginate @other_author
579       r.is_a?(Kaminari::PaginatableArray).should be_true
580     end
581     it '他作家の紙コマ一覧の取得条件を利用している' do
582       SheetPanel.stub(:himlist_where).with(any_args).and_return('')
583       SheetPanel.should_receive(:himlist_where).with(any_args).exactly(1)
584       r = SheetPanel.himlist_paginate @other_author
585     end
586     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
587       r = SheetPanel.himlist_paginate @other_author, 3, 10
588       r.limit_value.should eq 10
589       r.offset_value.should eq 20
590     end
591   end
592   
593   describe 'list関連テーブルプションに於いて' do
594     it '3つの項目を含んでいる' do
595       r = SheetPanel.list_opt
596       r.should have(3).items
597     end
598     it '用紙を含んでいる' do
599       r = SheetPanel.list_opt
600       r.has_key?(:sheet).should be_true
601     end
602       it '用紙は作家を含んでいる' do
603         r = SheetPanel.list_opt
604         r[:sheet].has_key?(:author).should be_true
605       end
606     it '作家を含んでいる' do
607       r = SheetPanel.list_opt
608       r.has_key?(:author).should be_true
609     end
610     it 'コマを含んでいる' do
611       r = SheetPanel.list_opt
612       r.has_key?(:panel).should be_true
613     end
614       it 'コマは作家を含んでいる' do
615         r = SheetPanel.list_opt
616         r[:panel].has_key?(:author).should be_true
617       end
618       it 'コマはコマ絵を含んでいる' do
619         r = SheetPanel.list_opt
620         r[:panel].has_key?(:panel_pictures).should be_true
621       end
622         it 'コマ絵は実素材を含んでいる' do
623           r = SheetPanel.list_opt
624           r[:panel][:panel_pictures].has_key?(:picture).should be_true
625         end
626           it '実素材は絵師を含んでいる' do
627             r = SheetPanel.list_opt
628             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
629           end
630           it '実素材はライセンスを含んでいる' do
631             r = SheetPanel.list_opt
632             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
633           end
634       it 'コマはフキダシを含んでいる' do
635         r = SheetPanel.list_opt
636         r[:panel].has_key?(:speech_balloons).should be_true
637       end
638         it 'フキダシはフキダシ枠を含んでいる' do
639           r = SheetPanel.list_opt
640           r[:panel][:speech_balloons].has_key?(:balloon).should be_true
641         end
642         it 'フキダシはセリフを含んでいる' do
643           r = SheetPanel.list_opt
644           r[:panel][:speech_balloons].has_key?(:speech).should be_true
645         end
646   end
647   describe 'json一覧出力オプションに於いて' do
648   end
649   
650   describe '単体取得に於いて' do
651     before do
652       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
653       @panel = FactoryGirl.create :panel, :author_id => @author.id
654       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
655     end
656     context 'つつがなく終わるとき' do
657       it '単体取得オプションを利用している' do
658         SheetPanel.stub(:show_opt).with(any_args).and_return({})
659         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
660         r = SheetPanel.show @sheet_panel.id, @author
661       end
662       it '閲覧許可を問い合わせている' do
663         SheetPanel.any_instance.stub(:visible?).with(@author).and_return(true)
664         SheetPanel.any_instance.should_receive(:visible?).with(@author).exactly(1)
665         r = SheetPanel.show @sheet_panel.id, @author
666       end
667     end
668     it '指定の紙コマを返す' do
669       l = SheetPanel.show @sheet_panel.id, @author
670       l.should eq @sheet_panel
671     end
672     context '他人の紙コマを開こうとしたとき' do
673       it '403Forbidden例外を返す' do
674         SheetPanel.any_instance.stub(:visible?).with(@other_author).and_return(false)
675         lambda{
676           SheetPanel.show @sheet_panel.id, @other_author
677         }.should raise_error(ActiveRecord::Forbidden)
678       end
679     end
680     context '存在しない紙コマを開こうとしたとき' do
681       it '404RecordNotFound例外を返す' do
682         lambda{
683           SheetPanel.show 110, @author
684         }.should raise_error(ActiveRecord::RecordNotFound)
685       end
686     end
687   end
688   
689   describe '編集取得に於いて' do
690     before do
691       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
692       @panel = FactoryGirl.create :panel, :author_id => @author.id
693       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
694     end
695     context 'つつがなく終わるとき' do
696       it '単体取得オプションを利用している' do
697         SheetPanel.stub(:show_opt).with(any_args).and_return({})
698         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
699         r = SheetPanel.edit @sheet_panel.id, @author
700       end
701       it '所持判定を問い合わせている' do
702         SheetPanel.any_instance.stub(:own?).with(any_args).and_return(true)
703         SheetPanel.any_instance.should_receive(:own?).with(any_args).exactly(1)
704         r = SheetPanel.edit @sheet_panel.id, @author
705       end
706     end
707     it '指定の紙コマを返す' do
708       l = SheetPanel.edit @sheet_panel.id, @author
709       l.should eq @sheet_panel
710     end
711     context '他人の紙コマを開こうとしたとき' do
712       it '403Forbidden例外を返す' do
713         SheetPanel.any_instance.stub(:own?).and_return(false)
714         lambda{
715           SheetPanel.edit @sheet_panel.id, @author
716         }.should raise_error(ActiveRecord::Forbidden)
717       end
718     end
719     context '存在しない紙コマを開こうとしたとき' do
720       it '404RecordNotFound例外を返す' do
721         lambda{
722           SheetPanel.edit 110, @author
723         }.should raise_error(ActiveRecord::RecordNotFound)
724       end
725     end
726   end
727   
728   describe '単体取得オプションに於いて' do
729     it 'includeキーを含んでいる' do
730       r = SheetPanel.show_opt
731       r.has_key?(:include).should be_true
732     end
733     it '3つの項目を含んでいる' do
734       r = SheetPanel.show_opt[:include]
735       r.should have(3).items
736     end
737     it '用紙を含んでいる' do
738       r = SheetPanel.show_opt[:include]
739       r.has_key?(:sheet).should be_true
740     end
741       it '用紙は作家を含んでいる' do
742         r = SheetPanel.show_opt[:include]
743         r[:sheet].has_key?(:author).should be_true
744       end
745     it '作家を含んでいる' do
746       r = SheetPanel.show_opt[:include]
747       r.has_key?(:author).should be_true
748     end
749     it 'コマを含んでいる' do
750       r = SheetPanel.show_opt[:include]
751       r.has_key?(:panel).should be_true
752     end
753       it 'コマは作家を含んでいる' do
754         r = SheetPanel.show_opt[:include]
755         r[:panel].has_key?(:author).should be_true
756       end
757       it 'コマはコマ絵を含んでいる' do
758         r = SheetPanel.show_opt[:include]
759         r[:panel].has_key?(:panel_pictures).should be_true
760       end
761         it 'コマ絵は実素材を含んでいる' do
762           r = SheetPanel.show_opt[:include]
763           r[:panel][:panel_pictures].has_key?(:picture).should be_true
764         end
765           it '実素材は絵師を含んでいる' do
766             r = SheetPanel.show_opt[:include]
767             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
768           end
769           it '実素材はライセンスを含んでいる' do
770             r = SheetPanel.show_opt[:include]
771             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
772           end
773       it 'コマはフキダシを含んでいる' do
774         r = SheetPanel.show_opt[:include]
775         r[:panel].has_key?(:speech_balloons).should be_true
776       end
777         it 'フキダシはフキダシ枠を含んでいる' do
778           r = SheetPanel.show_opt[:include]
779           r[:panel][:speech_balloons].has_key?(:balloon).should be_true
780         end
781         it 'フキダシはセリフを含んでいる' do
782           r = SheetPanel.show_opt[:include]
783           r[:panel][:speech_balloons].has_key?(:speech).should be_true
784         end
785   end
786   describe 'json単体取得オプションに於いて' do
787   end
788   
789   describe '紙コマのjson出力に於いて' do
790     before do
791       #コマを作成しておく。
792       @panel = FactoryGirl.create :panel, :author_id => @author.id
793       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
794       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
795       @speech = @sb.build_speech(
796         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
797       )
798       @balloon = @sb.build_balloon(
799         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
800       )
801       @sb.boost
802       @sb.save!
803       @gc = @panel.ground_colors.create(
804         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)
805       )
806       @gp = @panel.ground_pictures.create(
807         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
808       )
809       @panel.reload
810       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
811       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
812     end
813     context '事前チェックする' do
814       before do
815         Panel.any_instance.stub(:elements).and_return('{}')
816       end
817       it 'コマ要素のjson出力を依頼している' do
818         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
819         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
820         r = @sheet_panel.sheet_panel_as_json @author
821       end
822     end
823     it 'json textを返している' do
824       r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
825       r.is_a?(Hash).should be_true
826     end
827     it 'sheet,author,panel,コマ要素を含んでいる' do
828       r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
829       r.has_key?('sheet').should be_true
830       r['sheet'].has_key?('author').should be_true
831       r.has_key?('author').should be_true
832       r.has_key?('panel').should be_true
833       r['panel'].has_key?('author').should be_true
834     end
835     context 'コマ閲覧許可のとき' do
836       before do
837         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
838       end
839       it 'コマ要素にコマ要素を追加している' do
840         r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
841         r['panel'].has_key?('elements').should be_true
842         r['panel']['elements'].should_not be_empty
843       end
844     end
845     context 'コマ閲覧不許可のとき' do
846       before do
847         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
848       end
849       it 'コマ要素にデータを含ませない' do
850         r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
851         r['panel'].has_key?('elements').should be_false
852       end
853     end
854   end
855   
856   describe '紙コマリストのjson出力に於いて' do
857     before do
858       @panel = FactoryGirl.create :panel, :author_id => @author.id
859       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
860       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
861       SheetPanel.any_instance.stub(:sheet_panel_as_json).with(@author).and_return('{"s": 5}')
862     end
863     context 'つつがなく終わるとき' do
864       it '紙コマのjson出力を依頼している' do
865         SheetPanel.any_instance.should_receive(:sheet_panel_as_json).with(@author).exactly(1)
866         r = SheetPanel.list_as_json_text [@sheet_panel], @author
867       end
868     end
869     it 'json textを返している' do
870       r = SheetPanel.list_as_json_text [@sheet_panel], @author
871       j = JSON.parse r
872       j.is_a?(Array).should be_true
873     end
874     it '紙コマを含んでいる' do
875       r = SheetPanel.list_as_json_text [@sheet_panel], @author
876       j = JSON.parse r
877       j.first.has_key?('s').should be_true
878     end
879   end
880   
881   describe 't補充値に於いて' do
882     before do
883       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
884       @panel = FactoryGirl.create :panel, :author_id => @author.id
885     end
886     
887     context '用紙初のコマなら' do
888       it '0を補充値とする' do
889         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
890         @sheet_panel.t = nil
891         r = SheetPanel.new_t @sheet_panel.sheet_id
892         r.should eq 0
893       end
894     end
895     context '用紙に一個コマがあるとき' do
896       it '1を補充値とする' do
897         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
898         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
899         @sheet_panel.t = nil
900         r = SheetPanel.new_t @sheet_panel.sheet_id
901         r.should eq 1
902       end
903     end
904     context '用紙に2個コマがあるとき' do
905       it '2を補充値とする' do
906         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
907         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
908         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
909         @sheet_panel.t = nil
910         r = SheetPanel.new_t @sheet_panel.sheet_id
911         r.should eq 2
912       end
913     end
914   end
915   describe 'シリアライズチェックに於いて' do
916     context 'つつがなく終わるとき' do
917       it '0からシリアライズされているならTrueを返す' do
918         r = SheetPanel.serial? [0, 1, 2]
919         r.should be_true
920       end
921       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
922         r = SheetPanel.serial? [0, 2, 1]
923         r.should be_true
924       end
925       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
926         r = SheetPanel.serial? [ 2, 1, 4, 3, 0]
927         r.should be_true
928       end
929     end
930     context '異常なとき' do
931       it '0から始まらないならFalseを返す' do
932         r = SheetPanel.serial? [1, 2, 3]
933         r.should be_false
934       end
935       it '連続していないならFalseを返す' do
936         r = SheetPanel.serial? [0, 1, 2, 4]
937         r.should be_false
938       end
939       it '連続していないならFalseを返す' do
940         r = SheetPanel.serial? [0, 1, 2, 4, 5]
941         r.should be_false
942       end
943     end
944   end
945   describe 't収集に於いて' do
946     before do
947       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
948       @panel = FactoryGirl.create :panel, :author_id => @author.id
949       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
950       @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
951       @c2sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
952     end
953     context 'つつがなく終わるとき' do
954       it '紙コマから同一用紙のtだけを収集している' do
955         r = SheetPanel.collect_t @sheet_panel
956         r.should eq [0]
957       end
958     end
959     context '複数コマのとき' do
960       it '紙コマから同一用紙のtだけを収集している' do
961         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
962         r = SheetPanel.collect_t @sheet_panel
963         r.sort.should eq [0, 1]
964       end
965     end
966     context '複数コマでヨソの用紙も混じっているとき' do
967       it '紙コマから同一用紙のtだけを収集している' do
968         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
969         r = SheetPanel.collect_t @sheet_panel
970         r.sort.should eq [0, 1]
971       end
972     end
973   end
974   describe 'tチェックに於いて' do
975     before do
976       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
977       @panel = FactoryGirl.create :panel, :author_id => @author.id
978       @sheet_panel = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
979     end
980     context 'つつがなく終わるとき' do
981       it 't収集を依頼している' do
982         SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
983         SheetPanel.stub(:collect_t).with(any_args).and_return([])
984         SheetPanel.stub(:serial?).with(any_args).and_return(true)
985         r = SheetPanel.validate_t @sheet_panel
986       end
987       it '収集したtをシリアライズチェック依頼している' do
988         SheetPanel.stub(:collect_t).with(any_args).and_return([])
989         SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
990         SheetPanel.stub(:serial?).with(any_args).and_return(true)
991         r = SheetPanel.validate_t @sheet_panel
992       end
993     end
994     #実データでチェック
995     #依頼チェックだけでは不安なので最低限のチェックを
996     context '新規のとき' do
997       it '一件だけで正常通過している' do
998         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
999         r = SheetPanel.validate_t @sheet_panel
1000         r.should be_true 
1001       end
1002     end
1003     context '既存のとき' do
1004       it '2件目を作っても正常通過している' do
1005         @sheet_panel = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
1006         @sheet_panel2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
1007         r = SheetPanel.validate_t @sheet_panel2
1008         r.should be_true 
1009       end
1010     end
1011   end
1012   describe '挿入シフトに於いて' do
1013     before do
1014       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1015       @panel = FactoryGirl.create :panel, :author_id => @author.id
1016     end
1017     context '依頼チェック' do
1018       #テーブルが空で0に挿入
1019       it 'Updateを依頼している' do
1020         SheetPanel.stub(:update_all).with(any_args)
1021         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1022         @sheet_panel = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1023         @sheet_panel.insert_shift
1024       end
1025     end
1026     context 'テーブルに1件(t:0)で0に挿入したとき' do
1027       before do
1028         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1029         @sheet_panel2 = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1030       end
1031       it '既存の行を1にシフトしている' do
1032         @sheet_panel2.insert_shift
1033         l = SheetPanel.find :all
1034         l.first.t.should eq 1
1035       end
1036       it 'これから挿入するt(0)が欠番になっている' do
1037         @sheet_panel2.insert_shift
1038         l = SheetPanel.find(:all).map {|s| s.t }
1039         l.include?(0).should_not be_true
1040       end
1041     end
1042     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
1043       before do
1044         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1045         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1046         @sheet_panel3 = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1047       end
1048       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1049         @sheet_panel3.insert_shift
1050         l = SheetPanel.find(:all).map {|s| s.t }
1051         l.sort.should eq [0, 2]
1052       end
1053     end
1054     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
1055       before do
1056         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1057         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1058         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1059         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1060         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1061         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1062       end
1063       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1064         @sheet_panel6.insert_shift
1065         l = SheetPanel.find(:all).map {|s| s.t }
1066         l.sort.should eq [0, 1, 3, 4, 5]
1067       end
1068     end
1069     context '先ほどのケース+他の用紙1件で挿入したとき' do
1070       before do
1071         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1072         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1073         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1074         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1075         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1076         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1077         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1078         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1079       end
1080       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1081         @sheet_panel6.insert_shift
1082         l = SheetPanel.find(:all, :conditions => ['sheet_id = ?', @sheet.id]).map {|s| s.t }
1083         l.sort.should eq [0, 1, 3, 4, 5]
1084       end
1085       it '他の用紙に影響がない' do
1086         ot = @sheet_panelc2.t
1087         @sheet_panel6.insert_shift
1088         @sheet_panelc2.reload
1089         @sheet_panelc2.t.should eq ot
1090       end
1091     end
1092   end
1093   describe '少ない方に移動に於いて' do
1094     before do
1095       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1096       @panel = FactoryGirl.create :panel, :author_id => @author.id
1097     end
1098     context '依頼チェック' do
1099       it 'Updateを依頼している' do
1100         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1101         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1102         SheetPanel.stub(:update_all).with(any_args)
1103         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1104         ot = @sheet_panel2.t
1105         @sheet_panel2.t = 0
1106         @sheet_panel2.lesser_shift ot
1107       end
1108     end
1109     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
1110       before do
1111         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1112         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1113         @ot = @sheet_panel2.t
1114         @sheet_panel2.t = 0
1115       end
1116       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1117         #移動させたい行はそのまま残る
1118         @sheet_panel2.lesser_shift @ot
1119         l = SheetPanel.find(:all).map {|s| s.t }
1120         l.sort.should eq [1, 1]
1121       end
1122       it '既存のt0を1にシフトしている' do
1123         @sheet_panel2.lesser_shift @ot
1124         @sheet_panel.reload
1125         @sheet_panel.t.should eq 1
1126       end
1127     end
1128     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
1129       before do
1130         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1131         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1132         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1133         @ot = @sheet_panel3.t
1134         @sheet_panel3.t = 1
1135       end
1136       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1137         #移動させたい行はそのまま残る
1138         @sheet_panel3.lesser_shift @ot
1139         l = SheetPanel.find(:all).map {|s| s.t }
1140         l.sort.should eq [0, 2, 2]
1141       end
1142       it '既存のt1を2にシフトしている' do
1143         @sheet_panel3.lesser_shift @ot
1144         @sheet_panel2.reload
1145         @sheet_panel2.t.should eq 2
1146       end
1147     end
1148     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
1149       before do
1150         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1151         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1152         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1153         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1154         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1155         @ot = @sheet_panel4.t
1156         @sheet_panel4.t = 1
1157       end
1158       it 'これから挿入するt(1)が欠番になっている' do
1159         #移動させたい行はそのまま残る
1160         @sheet_panel4.lesser_shift @ot
1161         l = SheetPanel.find(:all).map {|s| s.t }
1162         l.sort.should eq [0, 2, 3, 3, 4]
1163       end
1164       it '既存のt0には変化がない' do
1165         @sheet_panel4.lesser_shift @ot
1166         @sheet_panel.reload
1167         @sheet_panel.t.should eq 0
1168       end
1169       it '既存のt4には変化がない' do
1170         @sheet_panel4.lesser_shift @ot
1171         @sheet_panel5.reload
1172         @sheet_panel5.t.should eq 4
1173       end
1174       it '既存のt1を2にシフトしている' do
1175         @sheet_panel4.lesser_shift @ot
1176         @sheet_panel2.reload
1177         @sheet_panel2.t.should eq 2
1178       end
1179       it '既存のt2を3にシフトしている' do
1180         @sheet_panel4.lesser_shift @ot
1181         @sheet_panel3.reload
1182         @sheet_panel3.t.should eq 3
1183       end
1184     end
1185     context '先ほどのケース+他の用紙1件で挿入したとき' do
1186       before do
1187         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1188         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1189         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1190         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1191         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1192         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1193         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1194         @ot = @sheet_panel4.t
1195         @sheet_panel4.t = 1
1196       end
1197       it 'これから挿入するt(1)が欠番になっている' do
1198         @sheet_panel4.lesser_shift @ot
1199         l = SheetPanel.find(:all).map {|s| s.t }
1200         l.sort.should eq [0, 0, 2, 3, 3, 4]
1201       end
1202       it '既存のt0には変化がない' do
1203         @sheet_panel4.lesser_shift @ot
1204         @sheet_panel.reload
1205         @sheet_panel.t.should eq 0
1206       end
1207       it '既存のt4には変化がない' do
1208         @sheet_panel4.lesser_shift @ot
1209         @sheet_panel5.reload
1210         @sheet_panel5.t.should eq 4
1211       end
1212       it '既存のt1を2にシフトしている' do
1213         @sheet_panel4.lesser_shift @ot
1214         @sheet_panel2.reload
1215         @sheet_panel2.t.should eq 2
1216       end
1217       it '既存のt2を3にシフトしている' do
1218         @sheet_panel4.lesser_shift @ot
1219         @sheet_panel3.reload
1220         @sheet_panel3.t.should eq 3
1221       end
1222       it '他の用紙に影響がない' do
1223         @sheet_panel4.lesser_shift @ot
1224         @sheet_panelc2.reload
1225         @sheet_panelc2.t.should eq 0
1226       end
1227     end
1228     #例外ケース。
1229     #負のときは0として正常扱い
1230     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
1231       before do
1232         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1233         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1234         @ot = @sheet_panel2.t
1235         @sheet_panel2.t = -1
1236       end
1237       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1238         #移動させたい行はそのまま残る
1239         @sheet_panel2.lesser_shift @ot
1240         l = SheetPanel.find(:all).map {|s| s.t }
1241         l.sort.should eq [1, 1]
1242       end
1243       it '既存のt0を1にシフトしている' do
1244         @sheet_panel2.lesser_shift @ot
1245         @sheet_panel.reload
1246         @sheet_panel.t.should eq 1
1247       end
1248       it '既存のt1は0に補正されている' do
1249         @sheet_panel2.lesser_shift @ot
1250         @sheet_panel2.t.should eq 0
1251       end
1252     end
1253   end
1254   describe '大きい方に移動に於いて' do
1255     before do
1256       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1257       @panel = FactoryGirl.create :panel, :author_id => @author.id
1258     end
1259     context '依頼チェック' do
1260       it 'Updateを依頼している' do
1261         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1262         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1263         SheetPanel.stub(:update_all).with(any_args)
1264         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1265         ot = @sheet_panel.t
1266         @sheet_panel.t = 1
1267         @sheet_panel.higher_shift ot
1268       end
1269     end
1270     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1271       before do
1272         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1273         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1274         @ot = @sheet_panel.t
1275         @sheet_panel.t = 1
1276       end
1277       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1278         #移動させたい行はそのまま残る
1279         @sheet_panel.higher_shift @ot
1280         l = SheetPanel.find(:all).map {|s| s.t }
1281         l.sort.should eq [0, 0]
1282       end
1283       it '既存のt1を0にシフトしている' do
1284         @sheet_panel.higher_shift @ot
1285         @sheet_panel2.reload
1286         @sheet_panel2.t.should eq 0
1287       end
1288     end
1289     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1290       before do
1291         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1292         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1293         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1294         @ot = @sheet_panel.t
1295         @sheet_panel.t = 1
1296       end
1297       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1298         #移動させたい行はそのまま残る
1299         @sheet_panel.higher_shift @ot
1300         l = SheetPanel.find(:all).map {|s| s.t }
1301         l.sort.should eq [0, 0, 2]
1302       end
1303       it '既存のt1を0にシフトしている' do
1304         @sheet_panel.higher_shift @ot
1305         @sheet_panel2.reload
1306         @sheet_panel2.t.should eq 0
1307       end
1308     end
1309     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1310       before do
1311         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1312         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1313         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1314         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1315         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1316         @ot = @sheet_panel2.t
1317         @sheet_panel2.t = 3
1318       end
1319       it 'これから挿入するt(3)が欠番になっている' do
1320         #移動させたい行はそのまま残る
1321         @sheet_panel2.higher_shift @ot
1322         l = SheetPanel.find(:all).map {|s| s.t }
1323         l.sort.should eq [0, 1, 1, 2, 4]
1324       end
1325       it '既存のt0には変化がない' do
1326         @sheet_panel2.higher_shift @ot
1327         @sheet_panel.reload
1328         @sheet_panel.t.should eq 0
1329       end
1330       it '既存のt4には変化がない' do
1331         @sheet_panel2.higher_shift @ot
1332         @sheet_panel5.reload
1333         @sheet_panel5.t.should eq 4
1334       end
1335       it '既存のt2を1にシフトしている' do
1336         @sheet_panel2.higher_shift @ot
1337         @sheet_panel3.reload
1338         @sheet_panel3.t.should eq 1
1339       end
1340       it '既存のt3を2にシフトしている' do
1341         @sheet_panel2.higher_shift @ot
1342         @sheet_panel4.reload
1343         @sheet_panel4.t.should eq 2
1344       end
1345     end
1346     context '先ほどのケース+他の用紙1件で挿入したとき' do
1347       before do
1348         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1349         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1350         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1351         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1352         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1353         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1354         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1355         @ot = @sheet_panel2.t
1356         @sheet_panel2.t = 3
1357       end
1358       it 'これから挿入するt(3)が欠番になっている' do
1359         #移動させたい行はそのまま残る
1360         @sheet_panel2.higher_shift @ot
1361         l = SheetPanel.find(:all).map {|s| s.t }
1362         l.sort.should eq [0, 0, 1, 1, 2, 4]
1363       end
1364       it '既存のt0には変化がない' do
1365         @sheet_panel2.higher_shift @ot
1366         @sheet_panel.reload
1367         @sheet_panel.t.should eq 0
1368       end
1369       it '既存のt4には変化がない' do
1370         @sheet_panel2.higher_shift @ot
1371         @sheet_panel5.reload
1372         @sheet_panel5.t.should eq 4
1373       end
1374       it '既存のt2を1にシフトしている' do
1375         @sheet_panel2.higher_shift @ot
1376         @sheet_panel3.reload
1377         @sheet_panel3.t.should eq 1
1378       end
1379       it '既存のt3を2にシフトしている' do
1380         @sheet_panel2.higher_shift @ot
1381         @sheet_panel4.reload
1382         @sheet_panel4.t.should eq 2
1383       end
1384       it '他の用紙に影響がない' do
1385         @sheet_panel2.higher_shift @ot
1386         @sheet_panelc2.reload
1387         @sheet_panelc2.t.should eq 0
1388       end
1389     end
1390     #例外ケース。
1391     #max超えたときはmaxとして正常扱い
1392     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1393       before do
1394         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1395         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1396         @ot = @sheet_panel.t
1397         @sheet_panel.t = 2
1398       end
1399       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1400         #移動させたい行はそのまま残る
1401         @sheet_panel.higher_shift @ot
1402         l = SheetPanel.find(:all).map {|s| s.t }
1403         l.sort.should eq [0, 0]
1404       end
1405       it '既存のt1を0にシフトしている' do
1406         @sheet_panel.higher_shift @ot
1407         @sheet_panel2.reload
1408         @sheet_panel2.t.should eq 0
1409       end
1410       it '既存のt0は1に補正されている' do
1411         @sheet_panel.higher_shift @ot
1412         @sheet_panel.t.should eq 1
1413       end
1414     end
1415   end
1416   describe '入れ替えに於いて' do
1417     before do
1418       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1419       @panel = FactoryGirl.create :panel, :author_id => @author.id
1420       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1421       @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1422     end
1423     context '新tが旧tより小さいとき' do
1424       it '少ない方に移動を依頼している' do
1425         SheetPanel.any_instance.stub(:lesser_shift).with(any_args)
1426         SheetPanel.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1427         ot = @sheet_panel2.t
1428         @sheet_panel2.t = 0
1429         @sheet_panel2.update_shift ot
1430       end
1431     end
1432     context '新tが旧tより大きいとき' do
1433       it '大きい方に移動を依頼している' do
1434         SheetPanel.any_instance.stub(:higher_shift).with(any_args)
1435         SheetPanel.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1436         ot = @sheet_panel.t
1437         @sheet_panel.t = 1
1438         @sheet_panel.update_shift ot
1439       end
1440     end
1441   end
1442   describe '順序入れ替えに於いて' do
1443     before do
1444       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1445       @panel = FactoryGirl.create :panel, :author_id => @author.id
1446     end
1447     context 'オブジェクトが新規でtが空のとき' do
1448       it '末尾追加としてtを補充依頼している' do
1449         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1450         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1451         SheetPanel.should_receive(:new_t).with(any_args).exactly(1)
1452         @sheet_panel.t = nil
1453         r = @sheet_panel.rotate
1454       end
1455     end
1456     context 'オブジェクトが新規でtが設定されているとき' do
1457       it '挿入追加として挿入シフトを依頼している' do
1458         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1459         SheetPanel.any_instance.stub(:insert_shift).with(any_args)
1460         SheetPanel.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1461         @sheet_panel.t = 0
1462         r = @sheet_panel.rotate
1463       end
1464     end
1465     context 'オブジェクトが新規でなくtが設定されているとき' do
1466       it 'コマ移動として入れ替えを依頼している' do
1467         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1468         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1469         SheetPanel.any_instance.stub(:update_shift).with(any_args)
1470         SheetPanel.any_instance.should_receive(:update_shift).with(1).exactly(1)
1471         @sheet_panel2.t = 0
1472         r = @sheet_panel.rotate 1
1473       end
1474     end
1475     context 'オブジェクトが新規でなくtが空のとき' do
1476       it '入れ替えもシフトもせず、tを空のままにしている' do
1477         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1478       end
1479     end
1480   end
1481   describe '編集許可に於いて' do
1482     before do
1483       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1484       @panel = FactoryGirl.create :panel, :author_id => @author.id
1485       @sheet_panel = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1486     end
1487     context 'つつがなく終わるとき' do
1488       it 'trueを返す' do
1489         r = @sheet_panel.allow?
1490         r.should be_true
1491       end
1492     end
1493     context '用紙で引っかかるとき' do
1494       it 'falseを返す' do
1495         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1496         Sheet.any_instance.stub(:own?).with(any_args).and_return(false)
1497         r = @sheet_panel.allow?
1498         r.should be_false
1499       end
1500     end
1501     context 'コマで引っかかるとき' do
1502       it 'falseを返す' do
1503         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1504         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1505         r = @sheet_panel.allow?
1506         r.should be_false
1507       end
1508     end
1509     context '用紙またはコマが指定されていなかったとき' do
1510       it 'nilを返す' do
1511         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1512         @sheet_panel.panel_id = nil
1513         r = @sheet_panel.allow?
1514         r.should eq nil
1515       end
1516       it 'nilを返す' do
1517         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1518         @sheet_panel.sheet_id = nil
1519         r = @sheet_panel.allow?
1520         r.should eq nil
1521       end
1522     end
1523   end
1524   describe '保存に於いて' do
1525     before do
1526       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1527       @panel = FactoryGirl.create :panel, :author_id => @author.id
1528       @sheet_panel = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1529     end
1530     context 'つつがなく終わるとき' do
1531       it '編集許可チェックを依頼している' do
1532         SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
1533         SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1534         r = @sheet_panel.store
1535       end
1536       it '順序入れ替えを依頼している' do
1537         SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
1538         SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1539         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1540         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1541         r = @sheet_panel.store 
1542       end
1543       it '保存を依頼している' do
1544         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1545         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1546         SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
1547         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1548         r = @sheet_panel.store
1549       end
1550       it 'tのシリアライズチェックを依頼している' do
1551         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1552         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1553         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1554         SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
1555         r = @sheet_panel.store
1556       end
1557     end
1558     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1559     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
1560       before do
1561         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1562         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1563         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1564         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1565         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1566         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1567         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1568         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1569       end
1570       it '既存のt0には変化がない' do
1571         @sheet_panel6.store
1572         @sheet_panel.reload
1573         @sheet_panel.t.should eq 0
1574       end
1575       it '既存のt1には変化がない' do
1576         @sheet_panel6.store
1577         @sheet_panel2.reload
1578         @sheet_panel2.t.should eq 1
1579       end
1580       it '既存のt2を3にシフトしている' do
1581         @sheet_panel6.store
1582         @sheet_panel3.reload
1583         @sheet_panel3.t.should eq 3
1584       end
1585       it '既存のt3を4にシフトしている' do
1586         @sheet_panel6.store
1587         @sheet_panel4.reload
1588         @sheet_panel4.t.should eq 4
1589       end
1590       it '既存のt5を5にシフトしている' do
1591         @sheet_panel6.store
1592         @sheet_panel5.reload
1593         @sheet_panel5.t.should eq 5
1594       end
1595       it '新規のt2が作成されている' do
1596         @sheet_panel6.store
1597         @sheet_panel6.reload
1598         @sheet_panel6.t.should eq 2
1599       end
1600       it '他の用紙に影響がない' do
1601         @ot = @sheet_panelc2.t
1602         @sheet_panel6.store
1603         @sheet_panelc2.reload
1604         @sheet_panelc2.t.should eq @ot
1605       end
1606     end
1607     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
1608       before do
1609         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1610         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1611         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1612         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1613         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1614         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1615         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1616         @ot = @sheet_panel4.t
1617         @sheet_panel4.t = 1
1618       end
1619       it '既存のt0には変化がない' do
1620         @sheet_panel4.store @ot
1621         @sheet_panel.reload
1622         @sheet_panel.t.should eq 0
1623       end
1624       it '既存のt4には変化がない' do
1625         @sheet_panel4.store @ot
1626         @sheet_panel5.reload
1627         @sheet_panel5.t.should eq 4
1628       end
1629       it '既存のt1を2にシフトしている' do
1630         @sheet_panel4.store @ot
1631         @sheet_panel2.reload
1632         @sheet_panel2.t.should eq 2
1633       end
1634       it '既存のt2を3にシフトしている' do
1635         @sheet_panel4.store @ot
1636         @sheet_panel3.reload
1637         @sheet_panel3.t.should eq 3
1638       end
1639       it '既存のt3を1にシフトしている' do
1640         @sheet_panel4.store @ot
1641         @sheet_panel4.reload
1642         @sheet_panel4.t.should eq 1
1643       end
1644       it '他の用紙に影響がない' do
1645         @sheet_panel4.store @ot
1646         @sheet_panelc2.reload
1647         @sheet_panelc2.t.should eq 0
1648       end
1649     end
1650     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
1651       before do
1652         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1653         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1654         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1655         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1656         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1657         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1658         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1659         @ot = @sheet_panel2.t
1660         @sheet_panel2.t = 3
1661       end
1662       it '既存のt0には変化がない' do
1663         @sheet_panel2.store @ot
1664         @sheet_panel.reload
1665         @sheet_panel.t.should eq 0
1666       end
1667       it '既存のt4には変化がない' do
1668         @sheet_panel2.store @ot
1669         @sheet_panel5.reload
1670         @sheet_panel5.t.should eq 4
1671       end
1672       it '既存のt1を3にシフトしている' do
1673         @sheet_panel2.store @ot
1674         @sheet_panel2.reload
1675         @sheet_panel2.t.should eq 3
1676       end
1677       it '既存のt2を1にシフトしている' do
1678         @sheet_panel2.store @ot
1679         @sheet_panel3.reload
1680         @sheet_panel3.t.should eq 1
1681       end
1682       it '既存のt3を2にシフトしている' do
1683         @sheet_panel2.store @ot
1684         @sheet_panel4.reload
1685         @sheet_panel4.t.should eq 2
1686       end
1687       it '他の用紙に影響がない' do
1688         @sheet_panel2.store @ot
1689         @sheet_panelc2.reload
1690         @sheet_panelc2.t.should eq 0
1691       end
1692     end
1693     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1694     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
1695       before do
1696         SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
1697         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1698         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1699         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1700         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1701         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1702         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1703         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1704         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1705       end
1706       it '既存のtに変化がない' do
1707         @sheet_panel6.store
1708         @sheet_panel.reload
1709         @sheet_panel.t.should eq 0
1710         @sheet_panel2.reload
1711         @sheet_panel2.t.should eq 1
1712         @sheet_panel3.reload
1713         @sheet_panel3.t.should eq 2
1714         @sheet_panel4.reload
1715         @sheet_panel4.t.should eq 3
1716         @sheet_panel5.reload
1717         @sheet_panel5.t.should eq 4
1718         @sheet_panelc2.reload
1719         @sheet_panelc2.t.should eq 0
1720       end
1721       it 'falseを返す' do
1722         r = @sheet_panel6.store
1723         r.should be_false
1724       end
1725     end
1726     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
1727       before do
1728         SheetPanel.stub(:validate_t).with(any_args).and_return(false)
1729         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1730         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1731         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1732         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1733         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1734         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1735         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1736         @ot = @sheet_panel4.t
1737         @sheet_panel4.t = 1
1738       end
1739       it '既存のtに変化がない' do
1740         @sheet_panel4.store @ot
1741         @sheet_panel.reload
1742         @sheet_panel.t.should eq 0
1743         @sheet_panel2.reload
1744         @sheet_panel2.t.should eq 1
1745         @sheet_panel3.reload
1746         @sheet_panel3.t.should eq 2
1747         @sheet_panel4.reload
1748         @sheet_panel4.t.should eq 3
1749         @sheet_panel5.reload
1750         @sheet_panel5.t.should eq 4
1751         @sheet_panelc2.reload
1752         @sheet_panelc2.t.should eq 0
1753       end
1754       it 'falseを返す' do
1755         r = @sheet_panel4.store @ot
1756         r.should be_false
1757       end
1758       it 'tにエラーメッセージが入っている' do
1759         @sheet_panel4.store @ot
1760         @sheet_panel4.errors[:t].should_not be_empty
1761         @sheet_panel4.valid?.should be_true
1762       end
1763     end
1764     context '編集不可だったとき' do
1765       before do
1766         @sheet_panel = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1767         SheetPanel.any_instance.stub(:allow?).and_return(false)
1768       end
1769       it '403Forbidden例外を返す' do
1770         lambda{
1771           @sheet_panel.store
1772         }.should raise_error(ActiveRecord::Forbidden)
1773       end
1774     end
1775   end
1776   describe '切り詰め処理つき削除に於いて' do
1777     before do
1778       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1779       @panel = FactoryGirl.create :panel, :author_id => @author.id
1780       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1781     end
1782     context 'つつがなく終わるとき' do
1783       it '削除される' do
1784         lambda{
1785           @sheet_panel.destroy_and_shorten
1786         }.should change(SheetPanel, :count ).by(-1)
1787       end
1788       it 'Trueを返す' do
1789         r = @sheet_panel.destroy_and_shorten
1790         r.should be_true 
1791       end
1792     end
1793     context '削除に失敗したとき' do
1794       before do
1795         SheetPanel.any_instance.stub(:destroy).and_return(false)
1796       end
1797       it 'ロールバックされる' do
1798         lambda{
1799           @sheet_panel.destroy_and_shorten
1800         }.should_not change(SheetPanel, :count )
1801       end
1802       it 'Falseを返す' do
1803         r = @sheet_panel.destroy_and_shorten
1804         r.should be_false
1805       end
1806     end
1807     #連携テスト。切り詰めが直接DBをいじる
1808     context '2件で先頭を削除したとき' do
1809       before do
1810         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1811       end
1812       it '行が削除される' do
1813         lambda{
1814           @sheet_panel.destroy_and_shorten
1815         }.should change(SheetPanel, :count ).by(-1)
1816       end
1817       it '先頭は削除される' do
1818         @sheet_panel.destroy_and_shorten
1819         lambda{
1820           SheetPanel.find @sheet_panel.id
1821         }.should raise_error(ActiveRecord::RecordNotFound)
1822       end
1823       it '2件目は前に詰められる' do
1824         @sheet_panel.destroy_and_shorten
1825         @sheet_panel2.reload
1826         @sheet_panel2.t.should eq 0
1827       end
1828     end
1829     context '3件で先頭を削除したとき' do
1830       before do
1831         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1832         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1833       end
1834       it '行が削除される' do
1835         lambda{
1836           @sheet_panel.destroy_and_shorten
1837         }.should change(SheetPanel, :count ).by(-1)
1838       end
1839       it '先頭は削除される' do
1840         @sheet_panel.destroy_and_shorten
1841         lambda{
1842           SheetPanel.find @sheet_panel.id
1843         }.should raise_error(ActiveRecord::RecordNotFound)
1844       end
1845       it '2件目は前に詰められる' do
1846         @sheet_panel.destroy_and_shorten
1847         @sheet_panel2.reload
1848         @sheet_panel2.t.should eq 0
1849       end
1850       it '3件目は前に詰められる' do
1851         @sheet_panel.destroy_and_shorten
1852         @sheet_panel3.reload
1853         @sheet_panel3.t.should eq 1
1854       end
1855     end
1856     context '5件で3件目を削除したとき' do
1857       before do
1858         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1859         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1860         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1861         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1862       end
1863       it '行が削除される' do
1864         lambda{
1865           @sheet_panel3.destroy_and_shorten
1866         }.should change(SheetPanel, :count ).by(-1)
1867       end
1868       it '1件目は変化がない' do
1869         @sheet_panel3.destroy_and_shorten
1870         @sheet_panel.reload
1871         @sheet_panel.t.should eq 0
1872       end
1873       it '2件目は変化がない' do
1874         @sheet_panel3.destroy_and_shorten
1875         @sheet_panel2.reload
1876         @sheet_panel2.t.should eq 1
1877       end
1878       it '3件目は削除される' do
1879         @sheet_panel3.destroy_and_shorten
1880         lambda{
1881           SheetPanel.find @sheet_panel3.id
1882         }.should raise_error(ActiveRecord::RecordNotFound)
1883       end
1884       it '4件目は前に詰められる' do
1885         @sheet_panel3.destroy_and_shorten
1886         @sheet_panel4.reload
1887         @sheet_panel4.t.should eq 2
1888       end
1889       it '5件目は前に詰められる' do
1890         @sheet_panel3.destroy_and_shorten
1891         @sheet_panel5.reload
1892         @sheet_panel5.t.should eq 3
1893       end
1894     end
1895     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1896   end
1897 end