OSDN Git Service

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