OSDN Git Service

t#31326:drop color
[pettanr/pettanr.git] / spec / models / panel_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 require 'spec_helper'\r
3 #コマ\r
4 describe Panel do\r
5   before do\r
6     @admin = FactoryGirl.create :admin\r
7     @sp = FactoryGirl.create :system_picture\r
8     @lg = FactoryGirl.create :license_group\r
9     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id\r
10     @user = FactoryGirl.create( :user_yas)\r
11     @author = FactoryGirl.create :author, :user_id => @user.id\r
12     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id\r
13     @other_user = FactoryGirl.create( :user_yas)\r
14     @other_author = FactoryGirl.create :author, :user_id => @other_user.id\r
15     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id\r
16     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
17     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
18     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
19     @sbt = FactoryGirl.create :speech_balloon_template\r
20   end\r
21   \r
22   describe '検証に於いて' do\r
23     before do\r
24       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
25     end\r
26     \r
27     context 'オーソドックスなデータのとき' do\r
28       it '下限データが通る' do\r
29         @panel.width = 1\r
30         @panel.height = 1\r
31         @panel.border = 0\r
32         @panel.x = -99999\r
33         @panel.y = -99999\r
34         @panel.z = 1\r
35         @panel.publish = 0\r
36         @panel.should be_valid\r
37       end\r
38       it '上限データが通る' do\r
39         @panel.width = 99999\r
40         @panel.height = 99999\r
41         @panel.border = 99999\r
42         @panel.x = 99999\r
43         @panel.y = 99999\r
44         @panel.z = 99999\r
45         @panel.publish = 99999\r
46         @panel.should be_valid\r
47       end\r
48     end\r
49     \r
50     context 'widthを検証するとき' do\r
51       it 'nullなら失敗する' do\r
52         @panel.width = nil\r
53         @panel.should_not be_valid\r
54       end\r
55       it '数値でなければ失敗する' do\r
56         @panel.width = 'a'\r
57         @panel.should_not be_valid\r
58       end\r
59       it '0なら失敗する' do\r
60         @panel.width = '0'\r
61         @panel.should_not be_valid\r
62       end\r
63       it '負でも失敗する' do\r
64         @panel.width = -1\r
65         @panel.should_not be_valid\r
66       end\r
67     end\r
68     context 'heightを検証するとき' do\r
69       it 'nullなら失敗する' do\r
70         @panel.height = nil\r
71         @panel.should_not be_valid\r
72       end\r
73       it '数値でなければ失敗する' do\r
74         @panel.height = 'a'\r
75         @panel.should_not be_valid\r
76       end\r
77       it '0なら失敗する' do\r
78         @panel.height = '0'\r
79         @panel.should_not be_valid\r
80       end\r
81       it '負でも失敗する' do\r
82         @panel.height = -1\r
83         @panel.should_not be_valid\r
84       end\r
85     end\r
86     context 'borderを検証するとき' do\r
87       it 'nullなら失敗する' do\r
88         @panel.border = nil\r
89         @panel.should_not be_valid\r
90       end\r
91       it '数値でなければ失敗する' do\r
92         @panel.border = 'a'\r
93         @panel.should_not be_valid\r
94       end\r
95       it '負なら失敗する' do\r
96         @panel.border = '-1'\r
97         @panel.should_not be_valid\r
98       end\r
99       it '0なら通る' do\r
100         @panel.border = 0\r
101         @panel.should be_valid\r
102       end\r
103     end\r
104     context 'xを検証するとき' do\r
105       it '数値でなければ失敗する' do\r
106         @panel.x = 'a'\r
107         @panel.should_not be_valid\r
108       end\r
109       it '0なら通る' do\r
110         @panel.x = '0'\r
111         @panel.should be_valid\r
112       end\r
113       it '負でも通る' do\r
114         @panel.x = -1\r
115         @panel.should be_valid\r
116       end\r
117     end\r
118     context 'yを検証するとき' do\r
119       it '数値でなければ失敗する' do\r
120         @panel.y = 'a'\r
121         @panel.should_not be_valid\r
122       end\r
123       it '0なら通る' do\r
124         @panel.y = '0'\r
125         @panel.should be_valid\r
126       end\r
127       it '負でも通る' do\r
128         @panel.y = -1\r
129         @panel.should be_valid\r
130       end\r
131     end\r
132     context 'zを検証するとき' do\r
133       it '数値でなければ失敗する' do\r
134         @panel.z = 'a'\r
135         @panel.should_not be_valid\r
136       end\r
137       it '0なら失敗する' do\r
138         @panel.z = '0'\r
139         @panel.should_not be_valid\r
140       end\r
141       it '負なら失敗する' do\r
142         @panel.z = -1\r
143         @panel.should_not be_valid\r
144       end\r
145     end\r
146     context 'author_idを検証するとき' do\r
147       it 'nullなら失敗する' do\r
148         @panel.author_id = nil\r
149         @panel.should_not be_valid\r
150       end\r
151       it '数値でなければ失敗する' do\r
152         @panel.author_id = 'a'\r
153         @panel.should_not be_valid\r
154       end\r
155       it '存在する作家でなければ失敗する' do\r
156         @panel.author_id = 0\r
157         @panel.should_not be_valid\r
158       end\r
159     end\r
160     context '公開フラグを検証するとき' do\r
161       it '数値でなければ失敗する' do\r
162         @panel.publish = 'a'\r
163         @panel.should_not be_valid\r
164       end\r
165     end\r
166     context '全体を検証するとき' do\r
167       before do\r
168         @panel = FactoryGirl.create :panel, :author_id => @author.id\r
169       end\r
170     end\r
171   end\r
172   \r
173   describe '文字コード検証に於いて' do\r
174     before do\r
175       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
176     end\r
177     \r
178     context 'captionを検証するとき' do\r
179       it 'Shift JISなら失敗する' do\r
180         @panel.caption = "\x83G\x83r\x83]\x83D"\r
181         lambda{\r
182           @panel.valid_encode\r
183         }.should raise_error(Pettanr::BadRequest)\r
184       end\r
185     end\r
186     \r
187   end\r
188   \r
189   describe 'デフォルト値補充に於いて' do\r
190     before do\r
191       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
192     end\r
193     it 'borderは2を補充する' do\r
194       @panel.border = nil\r
195       @panel.supply_default\r
196       @panel.border.should eq 2\r
197     end\r
198     it 'publishは0を補充する' do\r
199       @panel.publish = nil\r
200       @panel.supply_default\r
201       @panel.publish.should eq 0\r
202     end\r
203   end\r
204   \r
205   describe '上書き補充に於いて' do\r
206     before do\r
207       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
208     end\r
209     context 'author_idを補充' do\r
210       it 'ログイン中の作家idを補充する' do\r
211         @panel.author_id = nil\r
212         @panel.overwrite @author\r
213         @panel.author_id.should eq @author.id\r
214       end\r
215     end\r
216     \r
217   end\r
218   \r
219   describe '所持判定に於いて' do\r
220     before do\r
221       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
222     end\r
223     context '事前チェックする' do\r
224       it '自身にロールリストからの作家取得を依頼している' do\r
225         Panel.should_receive(:get_author_from_roles).with(any_args).exactly(1)\r
226         r = @panel.own?([@author])\r
227       end\r
228     end\r
229     context 'ロール内作家が取得できるとき' do\r
230       before do\r
231       end\r
232       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do\r
233         Panel.stub(:get_author_from_roles).with(any_args).and_return(@author)\r
234         r = @panel.own?([@author])\r
235         r.should be_true\r
236       end\r
237       it 'ロール内作家のidが自身の作家idと一致しないならno' do\r
238         Panel.stub(:get_author_from_roles).with(any_args).and_return(@other_author)\r
239         @panel.own?(@other_author).should be_false\r
240       end\r
241     end\r
242     context 'ロール内作家が取得できないとき' do\r
243       before do\r
244         Panel.stub(:get_author_from_roles).with(any_args).and_return(nil)\r
245       end\r
246       it 'Falseを返す' do\r
247         r = @panel.own?([@author])\r
248         r.should be_false\r
249       end\r
250     end\r
251   end\r
252   \r
253   describe '閲覧許可に於いて' do\r
254     before do\r
255       @comic = FactoryGirl.create :comic, :author_id => @author.id\r
256       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
257       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
258     end\r
259     context 'オープンモードのとき' do\r
260       before do\r
261         MagicNumber['run_mode'] = 0\r
262       end\r
263       it '自身にゲスト用ロールチェックを問い合わせしている' do\r
264         Panel.any_instance.stub(:guest_role_check).and_return(true)\r
265         Panel.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)\r
266         r = @panel.visible?([@author])\r
267       end\r
268       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do\r
269         Panel.any_instance.stub(:guest_role_check).and_return(false)\r
270         r = @panel.visible?([@author])\r
271         r.should be_false\r
272       end\r
273     end\r
274     context 'クローズドモードのとき' do\r
275       before do\r
276         MagicNumber['run_mode'] = 1\r
277       end\r
278       it '自身に読者用ロールチェックを問い合わせしている' do\r
279         Panel.any_instance.stub(:reader_role_check).and_return(true)\r
280         Panel.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)\r
281         r = @panel.visible?([@author])\r
282       end\r
283       it '読者用ロールチェックが失敗したとき、falseを返す' do\r
284         Panel.any_instance.stub(:reader_role_check).and_return(false)\r
285         r = @panel.visible?([@author])\r
286         r.should be_false\r
287       end\r
288     end\r
289     context '事前チェックする' do\r
290       before do\r
291         MagicNumber['run_mode'] = 1\r
292         Panel.any_instance.stub(:reader_role_check).and_return(true)\r
293       end\r
294       it '自身に所持判定を問い合わせしている' do\r
295         Panel.any_instance.stub(:own?).and_return(true)\r
296         Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
297         r = @panel.visible?([@author])\r
298       end\r
299       it '自身に閲覧許可を問い合わせしている' do\r
300         Panel.any_instance.stub(:own?).and_return(false)\r
301         Panel.any_instance.stub(:publish?).and_return(true)\r
302         Panel.any_instance.should_receive(:publish?).with(any_args).exactly(1)\r
303         r = @panel.visible?([@author])\r
304       end\r
305     end\r
306     context 'つつがなく終わるとき' do\r
307       before do\r
308         MagicNumber['run_mode'] = 1\r
309         Panel.any_instance.stub(:reader_role_check).and_return(true)\r
310       end\r
311       it '自分のコマなら許可する' do\r
312         Panel.any_instance.stub(:own?).and_return(true)\r
313         Panel.any_instance.stub(:publish?).and_return(false)\r
314         r = @panel.visible?([@author])\r
315         r.should be_true\r
316       end\r
317       it '他人の非公開コマなら許可しない' do\r
318         Panel.any_instance.stub(:own?).and_return(false)\r
319         Panel.any_instance.stub(:publish?).and_return(false)\r
320         r = @panel.visible?([@author])\r
321         r.should be_false\r
322       end\r
323       it '他人のコマでも公開なら許可する' do\r
324         Panel.any_instance.stub(:own?).and_return(false)\r
325         Panel.any_instance.stub(:publish?).and_return(true)\r
326         r = @panel.visible?([@author])\r
327         r.should be_true\r
328       end\r
329     end\r
330   end\r
331   \r
332   describe '一覧取得に於いて' do\r
333     before do\r
334       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
335     end\r
336     context 'page補正について' do\r
337       it '文字列から数値に変換される' do\r
338         Panel.page('8').should eq 8\r
339       end\r
340       it 'nilの場合は1になる' do\r
341         Panel.page().should eq 1\r
342       end\r
343       it '0以下の場合は1になる' do\r
344         Panel.page('0').should eq 1\r
345       end\r
346     end\r
347     context 'page_size補正について' do\r
348       it '文字列から数値に変換される' do\r
349         Panel.page_size('7').should eq 7\r
350       end\r
351       it 'nilの場合はPanel.default_page_sizeになる' do\r
352         Panel.page_size().should eq Panel.default_page_size\r
353       end\r
354       it '0以下の場合はPanel.default_page_sizeになる' do\r
355         Panel.page_size('0').should eq Panel.default_page_size\r
356       end\r
357       it 'Panel.max_page_sizeを超えた場合はPanel.max_page_sizeになる' do\r
358         Panel.page_size('1000').should eq Panel.max_page_size\r
359       end\r
360     end\r
361     it 'リストを返す' do\r
362       r = Panel.list\r
363       r.should eq [@panel]\r
364     end\r
365     it '時系列で並んでいる' do\r
366       #公開コミックは(他人のコミックであっても)含んでいる\r
367       npl = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100\r
368       r = Panel.list\r
369       r.should eq [npl, @panel]\r
370     end\r
371     it '非公開のコマ(自分のコマであっても)は含まない' do\r
372       npl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0\r
373       r = Panel.list\r
374       r.should eq [@panel]\r
375     end\r
376     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
377       before do\r
378         @npl2 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
379         @npl3 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 200\r
380         @npl4 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 300\r
381         @npl5 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 400\r
382         Panel.stub(:default_page_size).and_return(2)\r
383       end\r
384       it '通常は2件を返す' do\r
385         pl = Panel.list\r
386         pl.should have(2).items \r
387       end\r
388       it 'page=1なら末尾2件を返す' do\r
389         #時系列で並んでいる\r
390         pl = Panel.list 1\r
391         pl.should eq [@npl5, @npl4]\r
392       end\r
393       it 'page=2なら中間2件を返す' do\r
394         pl = Panel.list 2\r
395         pl.should eq [@npl3, @npl2]\r
396       end\r
397       it 'page=3なら先頭1件を返す' do\r
398         pl = Panel.list 3\r
399         pl.should eq [@panel]\r
400       end\r
401     end\r
402     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
403       before do\r
404         @npl2 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
405         @npl3 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 200\r
406         @npl4 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 300\r
407         @npl5 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 400\r
408         Panel.stub(:default_page_size).and_return(2)\r
409       end\r
410       it '件数0は全件(5件)を返す' do\r
411         r = Panel.list 5, 0\r
412         r.should have(5).items \r
413       end\r
414     end\r
415   end\r
416   \r
417   describe '自分のコマ一覧取得に於いて' do\r
418     before do\r
419       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
420     end\r
421     it 'リストを返す' do\r
422       pl = Panel.mylist @author\r
423       pl.should eq [@panel]\r
424     end\r
425     it '時系列で並んでいる' do\r
426       npl = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
427       pl = Panel.mylist @author\r
428       pl.should eq [npl, @panel]\r
429     end\r
430     it '他人のコマは公開でも含まない' do\r
431       npl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1\r
432       pl = Panel.mylist @author\r
433       pl.should eq [@panel]\r
434     end\r
435     it '自分のコマは非公開でも含んでいる' do\r
436       npl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0, :updated_at => Time.now + 100\r
437       pl = Panel.mylist @author\r
438       pl.should eq [npl, @panel]\r
439     end\r
440     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
441       before do\r
442         @npl2 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
443         @npl3 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 200\r
444         @npl4 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 300\r
445         @npl5 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 400\r
446       end\r
447       it '通常は2件を返す' do\r
448         pl = Panel.mylist @author, 1, 2\r
449         pl.should have(2).items \r
450       end\r
451       it 'page=1なら末尾2件を返す' do\r
452         #時系列で並んでいる\r
453         pl = Panel.mylist @author, 1, 2\r
454         pl.should eq [@npl5, @npl4]\r
455       end\r
456       it 'page=2なら中間2件を返す' do\r
457         pl = Panel.mylist @author, 2, 2\r
458         pl.should eq [@npl3, @npl2]\r
459       end\r
460       it 'page=3なら先頭1件を返す' do\r
461         pl = Panel.mylist @author, 3, 2\r
462         pl.should eq [@panel]\r
463       end\r
464     end\r
465     context 'DBに5件あって1ページの件数を0件に変えたとして' do\r
466       before do\r
467         @npl2 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
468         @npl3 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 200\r
469         @npl4 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 300\r
470         @npl5 = FactoryGirl.create :panel, :author_id => @author.id, :updated_at => Time.now + 400\r
471         Author.stub(:default_panel_page_size).and_return(2)\r
472       end\r
473       it '通常は全件(5件)を返す' do\r
474         r = Panel.mylist @author, 5, 0\r
475         r.should have(5).items \r
476       end\r
477     end\r
478   end\r
479   \r
480   describe '他作家のコマ一覧取得に於いて' do\r
481     before do\r
482       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
483       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1\r
484     end\r
485     it 'リストを返す' do\r
486       r = Panel.himlist @other_author\r
487       r.should eq [@other_panel]\r
488     end\r
489     it '時系列で並んでいる' do\r
490       npl = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100\r
491       r = Panel.himlist @other_author\r
492       r.should eq [npl, @other_panel]\r
493     end\r
494     it '公開コマに限る' do\r
495       npl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0\r
496       r = Panel.himlist @other_author\r
497       r.should eq [@other_panel]\r
498     end\r
499     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
500       before do\r
501         @other_panel2 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100\r
502         @other_panel3 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 200\r
503         @other_panel4 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 300\r
504         @other_panel5 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 400\r
505       end\r
506       it '通常は2件を返す' do\r
507         pl = Panel.himlist @other_author, 1, 2\r
508         pl.should have(2).items \r
509       end\r
510       it 'page=1なら末尾2件を返す' do\r
511         #時系列で並んでいる\r
512         pl = Panel.himlist @other_author, 1, 2\r
513         pl.should eq [@other_panel5, @other_panel4]\r
514       end\r
515       it 'page=2なら中間2件を返す' do\r
516         pl = Panel.himlist @other_author, 2, 2\r
517         pl.should eq [@other_panel3, @other_panel2]\r
518       end\r
519       it 'page=3なら先頭1件を返す' do\r
520         pl = Panel.himlist @other_author, 3, 2\r
521         pl.should eq [@other_panel]\r
522       end\r
523     end\r
524     context 'DBに5件あって1ページの件数を0件に変えたとして' do\r
525       before do\r
526         @other_panel2 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100\r
527         @other_panel3 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 200\r
528         @other_panel4 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 300\r
529         @other_panel5 = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 400\r
530         Author.stub(:default_panel_page_size).and_return(2)\r
531       end\r
532       it '通常は全件(5件)を返す' do\r
533         r = Panel.himlist @other_author, 5, 0\r
534         r.should have(5).items \r
535       end\r
536     end\r
537   end\r
538   \r
539   describe '一覧取得オプションに於いて' do\r
540     it 'includeキーを含んでいる' do\r
541       r = Panel.list_opt\r
542       r.has_key?(:include).should be_true\r
543     end\r
544     it '5つの項目を含んでいる' do\r
545       r = Panel.list_opt[:include]\r
546       r.should have(5).items\r
547     end\r
548     it 'コマ絵を含んでいる' do\r
549       r = Panel.list_opt[:include]\r
550       r.has_key?(:panel_pictures).should be_true\r
551     end\r
552       it 'コマ絵は実素材を含んでいる' do\r
553         r = Panel.list_opt[:include]\r
554         r[:panel_pictures].has_key?(:picture).should be_true\r
555       end\r
556         it '実素材は絵師を含んでいる' do\r
557           r = Panel.list_opt[:include]\r
558           r[:panel_pictures][:picture].has_key?(:artist).should be_true\r
559         end\r
560         it '実素材はライセンスを含んでいる' do\r
561           r = Panel.list_opt[:include]\r
562           r[:panel_pictures][:picture].has_key?(:license).should be_true\r
563         end\r
564     it 'フキダシを含んでいる' do\r
565       r = Panel.list_opt[:include]\r
566       r.has_key?(:speech_balloons).should be_true\r
567     end\r
568       it 'フキダシはフキダシ枠を含んでいる' do\r
569         r = Panel.list_opt[:include]\r
570         r[:speech_balloons].has_key?(:balloons).should be_true\r
571       end\r
572       it 'フキダシはセリフを含んでいる' do\r
573         r = Panel.list_opt[:include]\r
574         r[:speech_balloons].has_key?(:speeches).should be_true\r
575       end\r
576     it '絵地を含んでいる' do\r
577       r = Panel.list_opt[:include]\r
578       r.has_key?(:ground_pictures).should be_true\r
579     end\r
580       it '絵地は実素材を含んでいる' do\r
581         r = Panel.list_opt[:include]\r
582         r[:ground_pictures].has_key?(:picture).should be_true\r
583       end\r
584         it '実素材は絵師を含んでいる' do\r
585           r = Panel.list_opt[:include]\r
586           r[:ground_pictures][:picture].has_key?(:artist).should be_true\r
587         end\r
588         it '実素材はライセンスを含んでいる' do\r
589           r = Panel.list_opt[:include]\r
590           r[:ground_pictures][:picture].has_key?(:license).should be_true\r
591         end\r
592     it '色地を含んでいる' do\r
593       r = Panel.list_opt[:include]\r
594       r.has_key?(:ground_colors).should be_true\r
595     end\r
596     it '作家を含んでいる' do\r
597       r = Panel.list_opt[:include]\r
598       r.has_key?(:author).should be_true\r
599     end\r
600   end\r
601   describe 'json一覧出力オプションに於いて' do\r
602   end\r
603   \r
604   describe '単体取得に於いて' do\r
605     before do\r
606       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
607     end\r
608     context 'つつがなく終わるとき' do\r
609       it '単体取得オプションを利用している' do\r
610         Panel.stub(:show_opt).with(any_args).and_return({})\r
611         Panel.should_receive(:show_opt).with(any_args).exactly(1)\r
612         r = Panel.show @panel.id, @author\r
613       end\r
614       it '閲覧許可を問い合わせている' do\r
615         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)\r
616         Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
617         r = Panel.show @panel.id, @author\r
618       end\r
619     end\r
620     it '指定のコマを返す' do\r
621       Panel.any_instance.stub(:visible?).and_return(true)\r
622       pl = Panel.show @panel.id, @author\r
623       pl.should eq @panel\r
624     end\r
625     context '閲覧許可が出なかったとき' do\r
626       it '403Forbidden例外を返す' do\r
627         Panel.any_instance.stub(:visible?).and_return(false)\r
628         lambda{\r
629           Panel.show @panel.id, @author\r
630         }.should raise_error(ActiveRecord::Forbidden)\r
631       end\r
632     end\r
633     context '存在しないコマを開こうとしたとき' do\r
634       it '404RecordNotFound例外を返す' do\r
635         lambda{\r
636           Panel.show 110, @author\r
637         }.should raise_error(ActiveRecord::RecordNotFound)\r
638       end\r
639     end\r
640   end\r
641   describe '編集取得に於いて' do\r
642     before do\r
643       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
644     end\r
645     context 'つつがなく終わるとき' do\r
646       it '単体取得オプションを利用している' do\r
647         Panel.stub(:show_opt).with(any_args).and_return({})\r
648         Panel.should_receive(:show_opt).with(any_args).exactly(1)\r
649         r = Panel.edit @panel.id, @author\r
650       end\r
651       it '所持判定を問い合わせている' do\r
652         Panel.any_instance.stub(:own?).with(any_args).and_return(true)\r
653         Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
654         r = Panel.edit @panel.id, @author\r
655       end\r
656     end\r
657     it '指定のコマを返す' do\r
658       Panel.any_instance.stub(:own?).and_return(true)\r
659       pl = Panel.edit @panel.id, @author\r
660       pl.should eq @panel\r
661     end\r
662     context '他人のコマを開こうとしたとき' do\r
663       it '403Forbidden例外を返す' do\r
664         Panel.any_instance.stub(:own?).and_return(false)\r
665         lambda{\r
666           Panel.edit @panel.id, @author\r
667         }.should raise_error(ActiveRecord::Forbidden)\r
668       end\r
669     end\r
670     context '存在しないコマを開こうとしたとき' do\r
671       it '404RecordNotFound例外を返す' do\r
672         lambda{\r
673           Panel.edit 110, @author\r
674         }.should raise_error(ActiveRecord::RecordNotFound)\r
675       end\r
676     end\r
677   end\r
678   describe '単体取得オプションに於いて' do\r
679     it 'includeキーを含んでいる' do\r
680       r = Panel.show_opt\r
681       r.has_key?(:include).should be_true\r
682     end\r
683     it '5つの項目を含んでいる' do\r
684       r = Panel.show_opt[:include]\r
685       r.should have(5).items\r
686     end\r
687     it 'コマ絵を含んでいる' do\r
688       r = Panel.show_opt[:include]\r
689       r.has_key?(:panel_pictures).should be_true\r
690     end\r
691       it 'コマ絵は実素材を含んでいる' do\r
692         r = Panel.show_opt[:include]\r
693         r[:panel_pictures].has_key?(:picture).should be_true\r
694       end\r
695         it '実素材は絵師を含んでいる' do\r
696           r = Panel.show_opt[:include]\r
697           r[:panel_pictures][:picture].has_key?(:artist).should be_true\r
698         end\r
699         it '実素材はライセンスを含んでいる' do\r
700           r = Panel.show_opt[:include]\r
701           r[:panel_pictures][:picture].has_key?(:license).should be_true\r
702         end\r
703     it 'フキダシを含んでいる' do\r
704       r = Panel.show_opt[:include]\r
705       r.has_key?(:speech_balloons).should be_true\r
706     end\r
707       it 'フキダシはフキダシ枠を含んでいる' do\r
708         r = Panel.show_opt[:include]\r
709         r[:speech_balloons].has_key?(:balloons).should be_true\r
710       end\r
711       it 'フキダシはセリフを含んでいる' do\r
712         r = Panel.show_opt[:include]\r
713         r[:speech_balloons].has_key?(:speeches).should be_true\r
714       end\r
715     it '絵地を含んでいる' do\r
716       r = Panel.show_opt[:include]\r
717       r.has_key?(:ground_pictures).should be_true\r
718     end\r
719       it '絵地は実素材を含んでいる' do\r
720         r = Panel.show_opt[:include]\r
721         r[:ground_pictures].has_key?(:picture).should be_true\r
722       end\r
723         it '実素材は絵師を含んでいる' do\r
724           r = Panel.show_opt[:include]\r
725           r[:ground_pictures][:picture].has_key?(:artist).should be_true\r
726         end\r
727         it '実素材はライセンスを含んでいる' do\r
728           r = Panel.show_opt[:include]\r
729           r[:ground_pictures][:picture].has_key?(:license).should be_true\r
730         end\r
731     it '色地を含んでいる' do\r
732       r = Panel.show_opt[:include]\r
733       r.has_key?(:ground_colors).should be_true\r
734     end\r
735     it '作家を含んでいる' do\r
736       r = Panel.show_opt[:include]\r
737       r.has_key?(:author).should be_true\r
738     end\r
739   end\r
740   describe 'json単体出力オプションに於いて' do\r
741   end\r
742   describe 'コマ部品集合に於いて' do\r
743     before do\r
744       #コマを作成しておく。\r
745       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
746       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
747       @sb = @panel.speech_balloons.create(\r
748         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
749       )\r
750       @gc = @panel.ground_colors.create(\r
751         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
752       )\r
753       @gp = @panel.ground_pictures.create(\r
754         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
755       )\r
756       @panel.reload\r
757     end\r
758     it 'リストを返している' do\r
759       r = @panel.parts_element\r
760       r.is_a?(Array).should be_true\r
761     end\r
762     it 'コマ絵とフキダシを合わせている' do\r
763       r = @panel.parts_element\r
764       r.include?(@pp).should be_true\r
765       r.include?(@sb).should be_true\r
766     end\r
767     it '絵地と色地を含んでいない' do\r
768       r = @panel.parts_element\r
769       r.include?(@gc).should_not be_true\r
770       r.include?(@gp).should_not be_true\r
771     end\r
772   end\r
773   describe 'コマ部品に於いて' do\r
774     before do\r
775       #コマを作成しておく。\r
776       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
777       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
778       @sb = @panel.speech_balloons.create(\r
779         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
780       )\r
781       @gc = @panel.ground_colors.create(\r
782         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
783       )\r
784       @gp = @panel.ground_pictures.create(\r
785         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
786       )\r
787       @panel.reload\r
788     end\r
789     context 'つつがなく終わるとき' do\r
790       it 'コマ部品集合を利用している' do\r
791         Panel.any_instance.stub(:parts_element).with(any_args).and_return([])\r
792         Panel.any_instance.should_receive(:parts_element).with(any_args).exactly(1)\r
793         r = @panel.parts\r
794       end\r
795     end\r
796     it 'リストを返している' do\r
797       r = @panel.parts\r
798       r.is_a?(Array).should be_true\r
799       r.size.should eq 2\r
800     end\r
801     it 'tでソートしている' do\r
802       r = @panel.parts\r
803       r[0].should eq @sb\r
804       r[1].should eq @pp\r
805     end\r
806     context 'さらに末尾にフキダシを追加したとき' do\r
807       before do\r
808         @sb2 = @panel.speech_balloons.create(\r
809           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2)\r
810         )\r
811         @panel.reload\r
812       end\r
813       it 'tでソートしている' do\r
814         r = @panel.parts\r
815         r[0].should eq @sb\r
816         r[1].should eq @pp\r
817         r[2].should eq @sb2\r
818       end\r
819     end\r
820   end\r
821   describe 'コマ地に於いて' do\r
822     before do\r
823       #コマを作成しておく。\r
824       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
825       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
826       @sb = @panel.speech_balloons.create(\r
827         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
828       )\r
829       @gc = @panel.ground_colors.create(\r
830         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
831       )\r
832       @gp = @panel.ground_pictures.create(\r
833         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
834       )\r
835       @panel.reload\r
836     end\r
837     it 'リストを返している' do\r
838       r = @panel.grounds\r
839       r.is_a?(Array).should be_true\r
840     end\r
841     it '絵地と色地を合わせている' do\r
842       r = @panel.grounds\r
843       r.include?(@gc).should be_true\r
844       r.include?(@gp).should be_true\r
845     end\r
846     it 'コマ絵とフキダシを含んでいない' do\r
847       r = @panel.grounds\r
848       r.include?(@pp).should_not be_true\r
849       r.include?(@sb).should_not be_true\r
850     end\r
851   end\r
852   describe 'コマ要素に於いて' do\r
853     before do\r
854       #コマを作成しておく。\r
855       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
856       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
857       @sb = @panel.speech_balloons.create(\r
858         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
859       )\r
860       @gc = @panel.ground_colors.create(\r
861         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
862       )\r
863       @gp = @panel.ground_pictures.create(\r
864         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
865       )\r
866       @panel.reload\r
867     end\r
868     context 'つつがなく終わるとき' do\r
869       it 'コマ部品を利用している' do\r
870         Panel.any_instance.stub(:parts).with(any_args).and_return([])\r
871         Panel.any_instance.should_receive(:parts).with(any_args).exactly(1)\r
872         r = @panel.panel_elements\r
873       end\r
874       it 'コマ地を利用している' do\r
875         Panel.any_instance.stub(:grounds).with(any_args).and_return([])\r
876         Panel.any_instance.should_receive(:grounds).with(any_args).exactly(1)\r
877         r = @panel.panel_elements\r
878       end\r
879     end\r
880     it 'リストを返している' do\r
881       r = @panel.panel_elements\r
882       r.is_a?(Array).should be_true\r
883     end\r
884     it 'コマ絵とフキダシを合わせている' do\r
885       r = @panel.panel_elements\r
886       r.include?(@pp).should be_true\r
887       r.include?(@sb).should be_true\r
888     end\r
889     it '絵地と色地を合わせている' do\r
890       r = @panel.panel_elements\r
891       r.include?(@gc).should be_true\r
892       r.include?(@gp).should be_true\r
893     end\r
894     it 'tでソートしている[t順にソートできる部品の方が優先順位は高い。]' do\r
895       r = @panel.panel_elements\r
896       r[0].should eq @sb\r
897       r[1].should eq @pp\r
898     end\r
899   end\r
900   describe 'コマ要素のjson出力に於いて' do\r
901     before do\r
902       #コマを作成しておく。\r
903       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
904       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
905       @sb = @panel.speech_balloons.create(\r
906         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
907       )\r
908       @gc = @panel.ground_colors.create(\r
909         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
910       )\r
911       @gp = @panel.ground_pictures.create(\r
912         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
913       )\r
914       @panel.reload\r
915     end\r
916     context 'つつがなく終わるとき' do\r
917       before do\r
918         Panel.stub(:elm_json_opt).with(@pp).and_return({})\r
919         Panel.stub(:elm_json_opt).with(@sb).and_return({})\r
920         Panel.stub(:elm_json_opt).with(@gp).and_return({})\r
921         Panel.stub(:elm_json_opt).with(@gc).and_return({})\r
922       end\r
923       it 'コマ要素のjson出力オプションにコマ絵json出力オプションを依頼している' do\r
924         Panel.should_receive(:elm_json_opt).with(@pp).exactly(1)\r
925         r = @panel.elements\r
926       end\r
927       it 'コマ要素のjson出力オプションにフキダシjson出力オプションを依頼している' do\r
928         Panel.should_receive(:elm_json_opt).with(@sb).exactly(1)\r
929         r = @panel.elements\r
930       end\r
931       it 'コマ要素のjson出力オプションに絵地json出力オプションを依頼している' do\r
932         Panel.should_receive(:elm_json_opt).with(@gp).exactly(1)\r
933         r = @panel.elements\r
934       end\r
935       it 'コマ要素のjson出力オプションに色地json出力オプションを依頼している' do\r
936         Panel.should_receive(:elm_json_opt).with(@gc).exactly(1)\r
937         r = @panel.elements\r
938       end\r
939     end\r
940     it 'リストを返している' do\r
941       r = @panel.elements\r
942       r.is_a?(Array).should be_true\r
943     end\r
944   end\r
945   describe 'コマのjson出力に於いて' do\r
946     before do\r
947       #コマを作成しておく。\r
948       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
949       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
950       @sb = @panel.speech_balloons.create(\r
951         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
952       )\r
953       @gc = @panel.ground_colors.create(\r
954         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
955       )\r
956       @gp = @panel.ground_pictures.create(\r
957         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
958       )\r
959       @panel.reload\r
960     end\r
961     context 'つつがなく終わるとき' do\r
962       before do\r
963         Panel.any_instance.stub(:elements).with(any_args).and_return({})\r
964       end\r
965       it 'コマ要素のjson出力を依頼している' do\r
966         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)\r
967         r = @panel.panel_elements_as_json\r
968       end\r
969     end\r
970     it 'json textを返している' do\r
971       r = JSON.parse @panel.panel_elements_as_json\r
972       r.is_a?(Hash).should be_true\r
973     end\r
974     it 'author,コマ要素を含んでいる' do\r
975       r = JSON.parse @panel.panel_elements_as_json\r
976       r.has_key?('author').should be_true\r
977       r.has_key?('elements').should be_true\r
978       #t:0\r
979       sb = r['elements'].first\r
980       sb.has_key?('classname').should be_true\r
981       sb.has_key?('balloons').should be_true\r
982       sb.has_key?('speeches').should be_true\r
983       #t:1\r
984     end\r
985   end\r
986   describe 'コマリストのjson出力に於いて' do\r
987     before do\r
988       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
989       Panel.any_instance.stub(:panel_elements_as_json).with(any_args).and_return('{"p": 5}')\r
990     end\r
991     context 'つつがなく終わるとき' do\r
992       it 'コマのjson出力を依頼している' do\r
993         Panel.any_instance.should_receive(:panel_elements_as_json).with(any_args).exactly(1)\r
994         r = Panel.list_as_json_text [@panel]\r
995       end\r
996     end\r
997     it 'json textを返している' do\r
998       r = Panel.list_as_json_text [@panel]\r
999       j = JSON.parse r\r
1000       j.is_a?(Array).should be_true\r
1001     end\r
1002     it 'コマを含んでいる' do\r
1003       r = Panel.list_as_json_text [@panel]\r
1004       j = JSON.parse r\r
1005       j.first.has_key?('p').should be_true\r
1006     end\r
1007   end\r
1008   \r
1009   describe 'ライセンス素材に於いて' do\r
1010     before do\r
1011       #コマを作成しておく。\r
1012       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
1013       @p2 = FactoryGirl.create :picture, :original_picture_id => @op2.id, :license_id => @license.id, :artist_id => @artist.id\r
1014       @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p2.id\r
1015       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
1016       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height, :picture_id => @p.id\r
1017       @sb = @panel.speech_balloons.create(\r
1018         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
1019       )\r
1020       @gc = @panel.ground_colors.create(\r
1021         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
1022       )\r
1023       @gp = @panel.ground_pictures.create(\r
1024         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p2.id)\r
1025       )\r
1026       @panel.reload\r
1027     end\r
1028     context '事前チェック' do\r
1029     end\r
1030     context 'つつがなく終わるとき' do\r
1031     end\r
1032     it '連想配列(値は実素材、キーは実素材id)を返している' do\r
1033       r = @panel.licensed_pictures\r
1034       r.is_a?(Hash).should be_true\r
1035       r.should have(2).items\r
1036       r[@pp.picture_id].should eq @p\r
1037       r[@gp.picture_id].should eq @p2\r
1038     end\r
1039   end\r
1040   \r
1041   describe '検証値収集に於いて' do\r
1042     context 'つつがなく終わるとき' do\r
1043       it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do\r
1044         elements = [[{:panel_id => 1, :a => 'a'}, {:panel_id => 2, :a => 'a'}], \r
1045           [{:panel_id => 3, :a => 'a'}, {:panel_id => 4, :a => 'a'}]]\r
1046         r = Panel.collect_element_value elements, :panel_id\r
1047         r.should eq [1, 2, 3, 4]\r
1048       end\r
1049       it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do\r
1050         elements = [[{:t => 1, :a => 'a'}, {:t => 2, :a => 'a'}], \r
1051           [{:t => 3, :a => 'a'}, {:t => 0, :a => 'a'}]]\r
1052         r = Panel.collect_element_value elements, :t\r
1053         r.should eq [1, 2, 3, 0]\r
1054       end\r
1055     end\r
1056   end\r
1057   describe 'シリアライズチェックに於いて' do\r
1058     context 'つつがなく終わるとき' do\r
1059       it '0からシリアライズされているならTrueを返す' do\r
1060         r = Panel.validate_t [0, 1, 2]\r
1061         r.should be_true\r
1062       end\r
1063       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do\r
1064         r = Panel.validate_t [0, 2, 1]\r
1065         r.should be_true\r
1066       end\r
1067       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do\r
1068         r = Panel.validate_t [ 2, 1, 4, 3, 0]\r
1069         r.should be_true\r
1070       end\r
1071     end\r
1072     context '異常なとき' do\r
1073       it '0から始まらないならFalseを返す' do\r
1074         r = Panel.validate_t [1, 2, 3]\r
1075         r.should be_false\r
1076       end\r
1077       it '連続していないならFalseを返す' do\r
1078         r = Panel.validate_t [0, 1, 2, 4]\r
1079         r.should be_false\r
1080       end\r
1081       it '連続していないならFalseを返す' do\r
1082         r = Panel.validate_t [0, 1, 2, 4, 5]\r
1083         r.should be_false\r
1084       end\r
1085     end\r
1086   end\r
1087   describe 'tチェック単体に於いて' do\r
1088     before do\r
1089     end\r
1090     context 'つつがなく終わるとき' do\r
1091       it '検証値収集を依頼している' do\r
1092         Panel.should_receive(:collect_element_value).with(any_args).exactly(1)\r
1093         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
1094         Panel.stub(:validate_t).with(any_args).and_return(true)\r
1095         r = Panel.validate_element_t [], :t\r
1096       end\r
1097       it 'シリアライズチェック依頼している' do\r
1098         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
1099         Panel.should_receive(:validate_t).with(any_args).exactly(1)\r
1100         Panel.stub(:validate_t).with(any_args).and_return(true)\r
1101         r = Panel.validate_element_t [], :t\r
1102       end\r
1103     end\r
1104   end\r
1105   describe '従属データの検証に於いて' do\r
1106     context 'つつがなく終わるとき' do\r
1107       it 'trueを返している' do\r
1108         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1109         @panel.panel_pictures.build(\r
1110           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0)\r
1111         )\r
1112         @panel.panel_pictures.build(\r
1113           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1)\r
1114         )\r
1115         sb1 = @panel.speech_balloons.build(\r
1116           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2)\r
1117         )\r
1118         sb1.balloons.build(\r
1119           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
1120         )\r
1121         sb1.speeches.build(\r
1122           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
1123         )\r
1124         sb2 = @panel.speech_balloons.build(\r
1125           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3)\r
1126         )\r
1127         sb2.balloons.build(\r
1128           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
1129         )\r
1130         sb2.speeches.build(\r
1131           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
1132         )\r
1133         r = @panel.validate_child\r
1134         r.should be_true\r
1135       end\r
1136     end\r
1137     context 'シリアライズされていないとき' do\r
1138       it 'falseを返している' do\r
1139         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1140         @panel.panel_pictures.build(\r
1141           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 0)\r
1142         )\r
1143         @panel.panel_pictures.build(\r
1144           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 1)\r
1145         )\r
1146         sb1 = @panel.speech_balloons.build(\r
1147           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2)\r
1148         )\r
1149         sb1.balloons.build(\r
1150           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
1151         )\r
1152         sb1.speeches.build(\r
1153           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
1154         )\r
1155         sb2 = @panel.speech_balloons.build(\r
1156           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4)\r
1157         )\r
1158         sb2.balloons.build(\r
1159           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
1160         )\r
1161         sb2.speeches.build(\r
1162           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
1163         )\r
1164         r = @panel.validate_child\r
1165         r.should be_false\r
1166       end\r
1167     end\r
1168   end\r
1169   describe '保存に於いて' do\r
1170     before do\r
1171       @attr = FactoryGirl.attributes_for :panel\r
1172       @panel = Panel.new\r
1173       @panel.supply_default\r
1174     end\r
1175     context 'つつがなく終わるとき' do\r
1176       before do\r
1177         Panel.any_instance.stub(:validate_child).with(any_args).and_return(true)\r
1178         Panel.any_instance.stub(:save).with(any_args).and_return(true)\r
1179       end\r
1180       it 'コマモデルに上書き補充を依頼している' do\r
1181         Panel.any_instance.should_receive(:overwrite).exactly(1)\r
1182         r = @panel.store @attr, @author\r
1183       end\r
1184       it '従属データの検証を依頼している' do\r
1185         Panel.any_instance.should_receive(:validate_child).with(any_args).exactly(1)\r
1186         r = @panel.store @attr, @author\r
1187       end\r
1188       it '保存を依頼している' do\r
1189         Panel.any_instance.should_receive(:save).with(any_args).exactly(1)\r
1190         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1191         r = @panel.store @attr, @author\r
1192       end\r
1193     end\r
1194     context 'つつがなく終わるとき' do\r
1195       before do\r
1196         Panel.any_instance.stub(:validate_child).with(any_args).and_return(true)\r
1197       end\r
1198       it 'Trueを返す' do\r
1199         r = @panel.store @attr, @author\r
1200         r.should be_true\r
1201       end\r
1202       it '行が追加されている' do\r
1203         Panel.any_instance.stub(:validate_child).with(any_args).and_return(true)\r
1204         lambda {\r
1205           r = @panel.store @attr, @author\r
1206         }.should change(Panel, :count)\r
1207       end\r
1208     end\r
1209     context '不正なjsonデータのとき' do\r
1210       before do\r
1211       end\r
1212       it 'エラーメッセージがセットされている' do\r
1213         r = @panel.store false, @author\r
1214         @panel.errors[:base].should_not be_blank\r
1215       end\r
1216     end\r
1217     context '従属データの検証に失敗したとき' do\r
1218       before do\r
1219         Panel.any_instance.stub(:validate_child).with(any_args).and_return(false)\r
1220       end\r
1221       it 'エラーメッセージがセットされている' do\r
1222         r = @panel.store @attr, @author\r
1223         @panel.errors[:base].should_not be_blank\r
1224       end\r
1225     end\r
1226     context 'カラム値がFalseしたとき' do\r
1227       before do\r
1228         Panel.any_instance.stub(:validate_child).with(any_args).and_return(true)\r
1229       end\r
1230       it 'エラーメッセージがセットされている' do\r
1231         r = @panel.store false, @author\r
1232         @panel.errors.should_not be_empty\r
1233       end\r
1234     end\r
1235   end\r
1236   \r
1237   describe '削除に於いて' do\r
1238     before do\r
1239       @comic = FactoryGirl.create :comic, :author_id => @author.id\r
1240       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
1241       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
1242       @sb = @panel.speech_balloons.create(\r
1243         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
1244       )\r
1245       @gc = @panel.ground_colors.create(\r
1246         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)\r
1247       )\r
1248       @gp = @panel.ground_pictures.create(\r
1249         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
1250       )\r
1251     end\r
1252     context 'つつがなく終わるとき' do\r
1253       it '自身を削除する' do\r
1254         lambda {\r
1255           r = @panel.destroy_with_elements\r
1256         }.should change(Panel, :count).by(-1)\r
1257         lambda {\r
1258           r = Panel.find @panel.id\r
1259         }.should raise_error\r
1260       end\r
1261       it '自身にリンクしているコマ要素をすべて削除する' do\r
1262         lambda {\r
1263           r = @panel.destroy_with_elements\r
1264         }.should change(PanelPicture, :count).by(-1)\r
1265         lambda {\r
1266           r = PanelPicture.find @pp.id\r
1267         }.should raise_error \r
1268       end\r
1269       it '自身にリンクしているコマ要素をすべて削除する' do\r
1270         lambda {\r
1271           r = @panel.destroy_with_elements\r
1272         }.should change(GroundPicture, :count).by(-1)\r
1273         lambda {\r
1274           r = GroundPicture.find @gp.id\r
1275         }.should raise_error \r
1276       end\r
1277       it 'Trueを返す' do\r
1278         r = @panel.destroy_with_elements\r
1279         r.should be_true\r
1280       end\r
1281     end\r
1282     context '削除に失敗したとき' do\r
1283       before do\r
1284         PanelPicture.any_instance.stub(:destroy).with(any_args).and_return(false)\r
1285       end\r
1286       it 'Falseを返す' do\r
1287         r = @panel.destroy_with_elements\r
1288         r.should be_false\r
1289       end\r
1290       it 'ロールバックしている' do\r
1291         lambda {\r
1292           r = @panel.destroy_with_elements\r
1293         }.should_not change(Panel, :count)\r
1294         lambda {\r
1295           r = @panel.destroy_with_elements\r
1296         }.should_not change(PanelPicture, :count)\r
1297       end\r
1298     end\r
1299   end\r
1300 =begin\r
1301   describe 'id挿げ替え防止確認に於いて' do\r
1302     before do\r
1303       #コマを作成しておく。\r
1304       @panel = FactoryGirl.create :panel, :author_id => @author.id\r
1305       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
1306       @sb = @panel.speech_balloons.create(\r
1307         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
1308       )\r
1309       @gc = @panel.ground_colors.create(\r
1310         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :color_id => @color.id)\r
1311       )\r
1312       @gp = @panel.ground_pictures.create(\r
1313         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)\r
1314       )\r
1315       @pc = @panel.panel_colors.create(\r
1316         FactoryGirl.attributes_for(:panel_color, :panel_id => @panel.id, :code => 0xff0000)\r
1317       )\r
1318       @panel.reload\r
1319     end\r
1320     it 'author,コマ要素を含んでいる' do\r
1321       @panel2 = FactoryGirl.create :panel, :author_id => @author.id\r
1322       @panel2.panel_pictures.create(\r
1323         FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height)\r
1324       )\r
1325       @panel2.save!\r
1326     end\r
1327   end\r
1328   describe 'id一致チェックに於いて' do\r
1329     #parent.idがNilのときはすべてNil。数値のときは全一致\r
1330     context '親が既存(idが数値)のとき' do\r
1331       it 'すべてid一致するときTrueを返す' do\r
1332         r = Panel.validate_id [1, 1, 1, 1], 1\r
1333         r.should be_true\r
1334       end\r
1335       it 'idが一致する中にNilが混じってもTrueを返す' do\r
1336         r = Panel.validate_id [1, 1, 1, 1, nil], 1\r
1337         r.should be_true\r
1338       end\r
1339       it 'すべて数値でも一致しないときFalseを返す' do\r
1340         r = Panel.validate_id [1, 1, 1, 1, 2], 1\r
1341         r.should be_false\r
1342       end\r
1343       it '数値とNilが混ざってもIDが一致しないときFalseを返す' do\r
1344         r = Panel.validate_id [1, 1, nil, 2], 1\r
1345         r.should be_false\r
1346       end\r
1347     end\r
1348     context '親が新規(idがNil)のとき' do\r
1349       it 'すべてNilはTrueを返す' do\r
1350         r = Panel.validate_id [ nil,nil,nil,nil], nil\r
1351         r.should be_true\r
1352       end\r
1353       it 'すべてnilでなければFalseを返す' do\r
1354         r = Panel.validate_id [nil,nil,nil,nil, 2], nil\r
1355         r.should be_false\r
1356       end\r
1357     end\r
1358   end\r
1359   describe 'idチェック単体に於いて' do\r
1360     before do\r
1361 #      @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1\r
1362     end\r
1363     context 'つつがなく終わるとき' do\r
1364       it 'idを収集依頼している' do\r
1365         Panel.should_receive(:collect_element_value).with(any_args).exactly(1)\r
1366         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
1367         Panel.stub(:validate_id).with(any_args).and_return(true)\r
1368         r = Panel.validate_element_id [], :panel_id, nil\r
1369       end\r
1370       it '収集したidを一致チェック依頼している' do\r
1371         Panel.should_receive(:validate_id).with(any_args).exactly(1)\r
1372         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
1373         Panel.stub(:validate_id).with(any_args).and_return(true)\r
1374         r = Panel.validate_element_id [], :panel_id, nil\r
1375       end\r
1376     end\r
1377     #実データでチェック\r
1378     #依頼チェックだけでは不安なので最低限のチェックを\r
1379     context 'コマ新規のとき' do\r
1380       it 'コマ絵で正常通過している' do\r
1381         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1382         @panel.panel_pictures.build(\r
1383           FactoryGirl.attributes_for(:panel_picture, :panel_id => nil, :picture_id => @p.id, :t => nil)\r
1384         )\r
1385         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
1386         r.should be_true\r
1387       end\r
1388       it 'フキダシで正常通過している' do\r
1389         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1390         @panel.speech_balloons.build(\r
1391           FactoryGirl.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
1392         )\r
1393         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
1394         r.should be_true\r
1395       end\r
1396     end\r
1397     context 'コマ既存のとき' do\r
1398       it 'コマ絵で正常通過している' do\r
1399         @panel = FactoryGirl.create :panel, :author_id => @author.id\r
1400         @panel.panel_pictures.build(\r
1401           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => nil)\r
1402         )\r
1403         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
1404         r.should be_true\r
1405       end\r
1406       it 'フキダシで正常通過している' do\r
1407         @panel = FactoryGirl.create :panel, :author_id => @author.id\r
1408         @panel.speech_balloons.build(\r
1409           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
1410         )\r
1411         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
1412         r.should be_true\r
1413       end\r
1414     end\r
1415     context 'フキダシ新規のとき' do\r
1416       it 'バルーンで正常通過している' do\r
1417         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1418         @panel.speech_balloons.build(\r
1419           FactoryGirl.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
1420         )\r
1421         @panel.speech_balloons.first.balloons.build(\r
1422           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => nil)\r
1423         )\r
1424         r = Panel.validate_element_id [@panel.speech_balloons.first.speeches, @panel.speech_balloons.first.balloons], :speech_balloon_id, @panel.speech_balloons.first.id\r
1425         r.should be_true\r
1426       end\r
1427       it 'セリフで正常通過している' do\r
1428         @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1429         @panel.speech_balloons.build(\r
1430           FactoryGirl.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
1431         )\r
1432         @panel.speech_balloons.first.speeches.build(\r
1433           FactoryGirl.attributes_for(:speech, :speech_balloon_id => nil)\r
1434         )\r
1435         r = Panel.validate_element_id [@panel.speech_balloons.first.speeches, @panel.speech_balloons.first.balloons], :speech_balloon_id, @panel.speech_balloons.first.id\r
1436         r.should be_true\r
1437       end\r
1438     end\r
1439   end\r
1440   describe 'idチェックリスト生成に於いて' do\r
1441     before do\r
1442       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1443     end\r
1444     context 'つつがなく終わるとき' do\r
1445       it 'コマ部品とフキダシ部品のトータル2を返す' do\r
1446         @panel.panel_pictures.build(\r
1447           FactoryGirl.attributes_for(:panel_picture, :panel_id => nil, :picture_id => @p.id, :t => nil)\r
1448         )\r
1449         @panel.speech_balloons.build(\r
1450           FactoryGirl.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
1451         )\r
1452         @panel.speech_balloons.first.balloons.build(\r
1453           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => nil)\r
1454         )\r
1455         @panel.speech_balloons.first.speeches.build(\r
1456           FactoryGirl.attributes_for(:speech, :speech_balloon_id => nil)\r
1457         )\r
1458         r = @panel.validate_id_list\r
1459         r.should have(2).items\r
1460       end\r
1461       it 'コマ部品とフキダシ部品[複数:2]のケースでトータル3を返す' do\r
1462         @panel.panel_pictures.build(\r
1463           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => nil)\r
1464         )\r
1465         sb1 = @panel.speech_balloons.build(\r
1466           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
1467         )\r
1468         sb1.balloons.build(\r
1469           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
1470         )\r
1471         sb1.speeches.build(\r
1472           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
1473         )\r
1474         sb2 = @panel.speech_balloons.build(\r
1475           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
1476         )\r
1477         sb2.balloons.build(\r
1478           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
1479         )\r
1480         sb2.speeches.build(\r
1481           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
1482         )\r
1483         r = @panel.validate_id_list\r
1484         r.should have(3).items\r
1485       end\r
1486     end\r
1487   end\r
1488   describe 'idチェック複合に於いて' do\r
1489     before do\r
1490       @panel = FactoryGirl.build :panel, :author_id => @author.id\r
1491     end\r
1492     context 'つつがなく終わるとき' do\r
1493       it 'コマ部品とフキダシ部品の二回チェック依頼している' do\r
1494         Panel.should_receive(:validate_element_id).with(any_args).exactly(2)\r
1495         Panel.stub(:validate_element_id).with(any_args).and_return(true)\r
1496         @panel.panel_pictures.build(\r
1497           FactoryGirl.attributes_for(:panel_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => nil)\r
1498         )\r
1499         sb1 = @panel.speech_balloons.build(\r
1500           FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
1501         )\r
1502         sb1.balloons.build(\r
1503           FactoryGirl.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
1504         )\r
1505         sb1.speeches.build(\r
1506           FactoryGirl.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
1507         )\r
1508         r = Panel.validate_elements_id(@panel.validate_id_list)\r
1509       end\r
1510     end\r
1511   end\r
1512 =end\r
1513 end\r