OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
index 0ccc021..ede81d9 100644 (file)
@@ -1,37 +1,47 @@
 # -*- encoding: utf-8 -*-
+#原画
 require 'spec_helper'
 
 describe OriginalPicture do
   before do
-    Factory :admin
-    @user = Factory( :user_yas)
-    @author = @user.author
-    @artist = Factory :artist_yas, :author_id => @author.id
-    @other_user = Factory( :user_yas)
-    @other_author = @other_user.author
-    @other_artist = Factory :artist_yas, :author_id => @other_author.id
-    @sp = Factory :system_picture
-    @lg = Factory :license_group
-    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    SpeechBalloonTemplate.delete_all
+    @admin = FactoryGirl.create :admin
+    @user = FactoryGirl.create( :user_yas)
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
+    @other_user = FactoryGirl.create( :user_yas)
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+    @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
   end
   
   describe '検証に於いて' do
     before do
+      @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
-      @op.should be_valid
-    end
-    
-    context 'extを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @op.ext = 'png' #リストにない拡張子は通らないし
+        @op.width = 1
+        @op.height = 1
+        @op.filesize = 1
+        @op.md5 = 'a'*32
+        @op.should be_valid
       end
-      it 'テストデータの確認' do
+      it '上限データが通る' do
         @op.ext = 'jpeg'
+        @op.width = 99999
+        @op.height = 99999
+        @op.filesize = 2000000
+        @op.md5 = 'a'*32
         @op.should be_valid
       end
+    end
+    
+    context 'extを検証するとき' do
       it 'nullなら失敗する' do
         @op.ext = ''
         @op.should_not be_valid
@@ -46,13 +56,6 @@ describe OriginalPicture do
       end
     end
     context 'widthを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
-      end
-      it 'テストデータの確認' do
-        @op.width = 1
-        @op.should be_valid
-      end
       it 'nullなら失敗する' do
         @op.width = nil
         @op.should_not be_valid
@@ -71,13 +74,6 @@ describe OriginalPicture do
       end
     end
     context 'heightを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
-      end
-      it 'テストデータの確認' do
-        @op.height = 1
-        @op.should be_valid
-      end
       it 'nullなら失敗する' do
         @op.height = nil
         @op.should_not be_valid
@@ -96,13 +92,6 @@ describe OriginalPicture do
       end
     end
     context 'filesizeを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
-      end
-      it 'テストデータの確認' do
-        @op.filesize = 1
-        @op.should be_valid
-      end
       it 'nullなら失敗する' do
         @op.filesize = nil
         @op.should_not be_valid
@@ -121,13 +110,6 @@ describe OriginalPicture do
       end
     end
     context 'artist_idを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :license_id => @license.id
-      end
-      it 'テストデータの確認' do
-        @op.artist_id = @artist.id
-        @op.should be_valid
-      end
       it 'nullなら失敗する' do
         @op.artist_id = nil
         @op.should_not be_valid
@@ -141,100 +123,306 @@ describe OriginalPicture do
         @op.should_not be_valid
       end
     end
-    context 'license_idを検証するとき' do
-      before do
-        @op = Factory.build :original_picture, :artist_id => @artist.id
-      end
-      it 'テストデータの確認' do
-        @op.license_id = @license.id
-        @op.should be_valid
-      end
+    context 'md5を検証するとき' do
       it 'nullなら失敗する' do
-        @op.license_id = nil
+        @op.md5 = ''
         @op.should_not be_valid
       end
-      it '数値でなければ失敗する' do
-        @op.license_id = 'a'
+      it '31文字なら失敗する' do
+        @op.md5 = 'a'*31
         @op.should_not be_valid
       end
-      it '存在するlicenseでなければ失敗する' do
-        @op.license_id = 0
+      it '33文字なら失敗する' do
+        @op.md5 = 'a'*33
         @op.should_not be_valid
       end
     end
   end
   
-  describe 'データ補充に於いて' do
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
+      @op.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it '投稿日時が設定されている' do
+      @op = FactoryGirl.build :original_picture, :uploaded_at => nil
+      @op.overwrite @artist
+      @op.uploaded_at.should_not be_nil
+    end
+    it '絵師idが設定されている' do
+      @op = FactoryGirl.build :original_picture, :artist_id => nil
+      @op.overwrite @artist
+      @op.artist_id.should eq @artist.id
+    end
+  end
+  
+  describe '所持判定に於いて' do
     before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '事前チェックする' do
+      it '自身にロールリストからの絵師取得を依頼している' do
+        OriginalPicture.should_receive(:get_artist_from_roles).with(any_args).exactly(1)
+        r = @op.own?([@artist])
+      end
+    end
+    context 'ロール内絵師が取得できるとき' do
+      before do
+      end
+      it 'ロール内絵師のidが自身の絵師idと一致するなら許可する' do
+        OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(@artist)
+        r = @op.own?([@artist])
+        r.should be_true
+      end
+      it 'ロール内絵師のidが自身の絵師idと一致しないならno' do
+        OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(@other_artist)
+        @op.own?(@other_artist).should be_false
+      end
+    end
+    context 'ロール内絵師が取得できないとき' do
+      before do
+        OriginalPicture.stub(:get_artist_from_roles).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @op.own?([@artist])
+        r.should be_false
+      end
     end
-    
   end
   
-  describe '作者判定に於いて' do
+  describe '閲覧許可に於いて' do
     before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '事前チェックする' do
+      before do
+        MagicNumber['run_mode'] = 1
+      end
+      it '自身の原画に所持判定を問い合わせしている' do
+        OriginalPicture.any_instance.stub(:own?).and_return(true)
+        OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
+        r = @op.visible?([@artist])
+      end
     end
-    it '自作の原画ならyes' do
-      op = Factory :original_picture, :artist_id => @artist.id
-      op.own?(@author).should == true
+    context 'つつがなく終わるとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+      end
+      it '自分の原画なら許可する' do
+        OriginalPicture.any_instance.stub(:own?).and_return(true)
+        r = @op.visible?([@artist])
+        r.should be_true
+      end
+      it '他人の原画なら許可しない' do
+        OriginalPicture.any_instance.stub(:own?).and_return(false)
+        r = @op.visible?([@artist])
+        r.should be_false
+      end
     end
-    it '他人のならno' do
-      op = Factory :original_picture, :artist_id => @other_artist.id
-      op.own?(@author).should == false
+  end
+  
+  describe 'ファイル名に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
-    it '作家が不明ならno' do
-      op = Factory :original_picture, :artist_id => @artist.id
-      op.own?(nil).should == false
+    it 'id+拡張子のフォーマットで返す' do
+      r = @op.filename
+      r.should eq "#{@op.id}.png"
     end
-    it '作家が絵師でないならno' do
-      other_user = Factory( :user_yas)
-      op = Factory :original_picture, :artist_id => @artist.id
-      op.own?(@other_author).should == false
+  end
+  
+  describe 'MimeTypeに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    it 'image/拡張子のフォーマットで返す' do
+      r = @op.mime_type
+      r.should eq "image/png"
     end
   end
-  describe '単体取得に於いて' do
+  
+  describe 'ファイルのurlに於いて' do
     before do
-      @op = Factory :original_picture, :artist_id => @artist.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
     end
-    it '指定の原画を返す' do
-      pic = OriginalPicture.show @op.id, @artist
-      pic.should eq @op
+    it 'ファイル名取得を依頼している' do
+      OriginalPicture.any_instance.should_receive(:filename).exactly(1)
+      @op.url
+    end
+    it '/original_pictures/3.gifのフォーマットで返す' do
+      r = @op.url
+      r.should eq "/original_pictures/3.gif"
     end
-    context '関連テーブルオプションがないとき' do
-      it 'ライセンスと素材を含んでいる' do
-        r = OriginalPicture.show_include_opt
-        r.should eq [:license, :resource_picture]
+  end
+  
+  describe 'サムネイル画像タグオプションに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
+      PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
+    end
+    it 'サムネイル画像の幅高さ取得を依頼している' do
+      PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
+      @op.tmb_opt_img_tag
+    end
+    it '戻り値はHashで返す' do
+      r = @op.tmb_opt_img_tag
+      r.is_a?(Hash).should be_true
+    end
+    it 'srcキーを含んでいる' do
+      r = @op.tmb_opt_img_tag
+      r.has_key?(:src).should be_true
+      r[:src].should eq '/original_pictures/3.gif'
+    end
+    it 'widthキーを含んでいる' do
+      r = @op.tmb_opt_img_tag
+      r.has_key?(:width).should be_true
+      r[:width].should eq 40
+    end
+    it 'heightキーを含んでいる' do
+      r = @op.tmb_opt_img_tag
+      r.has_key?(:height).should be_true
+      r[:height].should eq 30
+    end
+  end
+  
+  describe '画像タグオプションに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
+    end
+    it '戻り値はHashで返す' do
+      r = @op.opt_img_tag
+      r.is_a?(Hash).should be_true
+    end
+    it 'srcキーを含んでいる' do
+      r = @op.opt_img_tag
+      r.has_key?(:src).should be_true
+      r[:src].should eq '/original_pictures/3.gif'
+    end
+    it 'widthキーを含んでいる' do
+      r = @op.opt_img_tag
+      r.has_key?(:width).should be_true
+      r[:width].should eq @op.width
+    end
+    it 'heightキーを含んでいる' do
+      r = @op.opt_img_tag
+      r.has_key?(:height).should be_true
+      r[:height].should eq @op.height
+    end
+  end
+  
+  describe '未公開に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '公開日時と停止日時がともに空のとき' do
+      it 'Trueを返す' do
+        r = @op.unpublished?
+        r.should be_true
       end
     end
-    context '関連テーブルオプションで絵師を含ませたとき' do
-      it 'ライセンスと素材と作者データを含んでいる' do
-        r = OriginalPicture.show_include_opt(:include => :artist)
-        r.should eq [:license, :resource_picture, :artist]
+    context '公開日時と停止日時がともに空ではないとき' do
+      it 'Falseを返す' do
+        @op.stopped_at =Time.now
+        r = @op.unpublished?
+        r.should be_false
+      end
+      it 'Falseを返す' do
+        @op.published_at =Time.now
+        r = @op.unpublished?
+        r.should be_false
       end
     end
-    context '他人の原画を開こうとしたとき' do
-      it '403Forbidden例外を返す' do
-        lambda{
-          pic = OriginalPicture.show @op.id, @other_artist
-        }.should raise_error(ActiveRecord::Forbidden)
+  end
+
+  describe '停止中に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '停止日時が空でないとき' do
+      it 'Trueを返す' do
+        @op.stopped_at =Time.now
+        r = @op.stopped?
+        r.should be_true
       end
     end
-    context 'å­\98å\9c¨ã\81\97ã\81ªã\81\84å\8e\9fç\94»ã\82\92é\96\8bã\81\93ã\81\86ã\81¨ã\81\97ã\81\9fとき' do
-      it '404RecordNotFound例外を返す' do
-        lambda{
-          pic = OriginalPicture.show 0, @artist
-        }.should raise_error(ActiveRecord::RecordNotFound)
+    context 'å\81\9cæ­¢æ\97¥æ\99\82ã\81\8c空ã\81®とき' do
+      it 'Falseを返す' do
+        @op.stopped_at = nil
+        r = @op.stopped?
+        r.should be_false
       end
     end
   end
-  describe 'json単体出力オプションに於いて' do
-    it 'includeキーがライセンスと素材を含んでいる' do
-      r = OriginalPicture.show_json_include_opt
-      r[:include].should eq [:license, :resource_picture]
+
+  describe 'ライセンス待ちに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '公開日時と停止日時のうち、空でない方を取得して、投稿日時がそれより後のとき' do
+      it 'Trueを返す' do
+        @op.uploaded_at = Time.now
+        @op.stopped_at = Time.now - 400
+        r = @op.unlicensed?
+        r.should be_true
+      end
+      it 'Trueを返す' do
+        @op.uploaded_at = Time.now
+        @op.published_at =Time.now - 400
+        r = @op.unlicensed?
+        r.should be_true
+      end
+    end
+    context '公開日時と停止日時のうち、空でない方を取得して、投稿日時がそれより後ではないとき' do
+      it 'Falseを返す' do
+        @op.published_at = nil
+        @op.stopped_at = nil
+        r = @op.unlicensed?
+        r.should be_false
+      end
+      it 'Falseを返す' do
+        @op.uploaded_at = Time.now
+        @op.published_at =Time.now + 400
+        r = @op.unlicensed?
+        r.should be_false
+      end
+      it 'Falseを返す' do
+        @op.uploaded_at = Time.now
+        @op.stopped_at =Time.now + 400
+        r = @op.unlicensed?
+        r.should be_false
+      end
+    end
+  end
+
+  describe '公開中に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '公開日時が空ではないとき' do
+      it 'Trueを返す' do
+        @op.published_at =Time.now
+        r = @op.published?
+        r.should be_true
+      end
+    end
+    context '公開日時が空のとき' do
+      it 'Falseを返す' do
+        @op.published_at = nil
+        r = @op.published?
+        r.should be_false
+      end
     end
   end
+
   describe '一覧取得に於いて' do
     before do
-      @op = Factory :original_picture, :artist_id => @artist.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
     context 'page補正について' do
       it '文字列から数値に変換される' do
@@ -261,225 +449,568 @@ describe OriginalPicture do
         OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
       end
     end
-    it 'リストを返す' do
-      pic = OriginalPicture.list @artist.id
-      pic.should eq [@op]
+  end
+  
+  describe '自分の原画一覧取得に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
-    it '他人の原画は含んでいない' do
-      Factory :original_picture, :artist_id => @other_artist.id
-      pic = OriginalPicture.list @artist.id
-      pic.should eq [@op]
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        OriginalPicture.stub(:list_opt).with(any_args).and_return({})
+        OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
+        r = OriginalPicture.mylist @artist
+      end
+    end
+    it 'リストを返す' do
+      r = OriginalPicture.mylist @artist
+      r.should eq [@op]
     end
     it '時系列で並んでいる' do
-      newpic = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
-      pic = OriginalPicture.list @artist.id
-      pic.should eq [newpic, @op]
+      nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
+      r = OriginalPicture.mylist @artist
+      r.should eq [nc, @op]
+    end
+    it '他人の原画は含まない' do
+      nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      r = OriginalPicture.mylist @artist
+      r.should eq [@op]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @op2 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
-        @op3 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
-        @op4 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
-        @op5 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
-        OriginalPicture.stub(:default_page_size).and_return(2)
+        @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
+        @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
+        @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
+        @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
       end
       it '通常は2件を返す' do
-        pic = OriginalPicture.list @artist.id
-        pic.should have(2).items 
+        r = OriginalPicture.mylist @artist, 1, 2
+        r.should have(2).items 
       end
       it 'page=1なら末尾2件を返す' do
         #時系列で並んでいる
-        pic = OriginalPicture.list( @artist.id, {}, 1)
-        pic.should eq [@op5, @op4]
+        r = OriginalPicture.mylist @artist, 1, 2
+        r.should eq [@op5, @op4]
       end
       it 'page=2なら中間2件を返す' do
-        pic = OriginalPicture.list( @artist.id, {}, 2)
-        pic.should eq [@op3, @op2]
+        r = OriginalPicture.mylist @artist, 2, 2
+        r.should eq [@op3, @op2]
       end
       it 'page=3なら先頭1件を返す' do
-        pic = OriginalPicture.list( @artist.id, {}, 3)
-        pic.should eq [@op]
+        r = OriginalPicture.mylist @artist, 3, 2
+        r.should eq [@op]
       end
     end
   end
+  
+  describe '自分の原画一覧ページ制御に於いて' do
+    before do
+      OriginalPicture.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = OriginalPicture.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分の原画一覧の取得条件を利用している' do
+      OriginalPicture.stub(:mylist_where).with(any_args).and_return('')
+      OriginalPicture.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = OriginalPicture.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = OriginalPicture.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '一覧取得オプションに於いて' do
+    it '2つの項目を含んでいる' do
+      r = OriginalPicture.list_opt
+      r.should have(2).items
+    end
+    it '素材を含んでいる' do
+      r = OriginalPicture.list_opt
+      r.has_key?(:resource_picture).should be_true
+    end
+    it '実素材を含んでいる' do
+      r = OriginalPicture.list_opt
+      r.has_key?(:pictures).should be_true
+    end
+  end
   describe 'json一覧出力オプションに於いて' do
-    it 'includeキーがライセンスと素材を含んでいる' do
-      r = OriginalPicture.list_json_opt
-      r[:include].should eq [:license, :resource_picture]
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      @sbt = FactoryGirl.create :speech_balloon_template
+      @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
+    end
+    it '素材を含んでいる' do
+      r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('resource_picture').should be_true
+    end
+    it '実素材を含んでいる' do
+      r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('pictures').should be_true
+    end
+  end
+  
+  describe '更新履歴一覧取得に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
+        :original_picture_id => @op.id
+      @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    it 'リストを返す' do
+      r = @op.history
+      r.should eq [@p]
+    end
+    it '他の原画の実素材は含んでいない' do
+      @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
+        :original_picture_id => @op2.id
+      r = @op.history
+      r.should eq [@p]
+    end
+    it 'revisionで並んでいる' do
+      @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
+        :original_picture_id => @op.id
+      r = @op.history
+      r.should eq [@p2, @p]
     end
   end
-  describe 'RMagick変換に於いて' do
+  
+  describe '単体取得に於いて' do
     before do
-      @op = Factory.build :original_picture, :artist_id => @artist.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
     context 'つつがなく終わるとき' do
-      it '画像データをオブジェクト化している' do
-        Magick::Image.stub(:from_blob).and_return(['mgkobj'])
-        @op.data_to_mgk('mgkbin').should eq 'mgkobj'
+      it '単体取得オプションを利用している' do
+        OriginalPicture.stub(:show_opt).with(any_args).and_return({})
+        OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
+        r = OriginalPicture.show @op.id, @artist
+      end
+      it '閲覧許可を問い合わせている' do
+        OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
+        OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = OriginalPicture.show @op.id, @artist
       end
     end
-    context '失敗するとき' do
-      it 'Falseを返す' do
-        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
-        @op.data_to_mgk('mgkbin').should be_false
+    it '指定の原画を返す' do
+      pic = OriginalPicture.show @op.id, @artist
+      pic.should eq @op
+    end
+    context '他人の原画を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        OriginalPicture.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          pic = OriginalPicture.show @op.id, @other_artist
+        }.should raise_error(ActiveRecord::Forbidden)
       end
-      it '全体エラーがセットされている' do
-        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
-        lambda {
-          @op.data_to_mgk('mgkbin')
-        }.should change(@op.errors[:base], :count)
+    end
+    context '存在しない原画を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          pic = OriginalPicture.show 0, @artist
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '編集取得に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        OriginalPicture.stub(:show_opt).with(any_args).and_return({})
+        OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
+        r = OriginalPicture.edit @op.id, @artist
+      end
+      it '所持判定を問い合わせている' do
+        OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
+        OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
+        r = OriginalPicture.edit @op.id, @artist
+      end
+    end
+    it '指定の原画を返す' do
+      pic = OriginalPicture.edit @op.id, @artist
+      pic.should eq @op
+    end
+    context '他人の原画を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        OriginalPicture.any_instance.stub(:own?).and_return(false)
+        lambda{
+          r = OriginalPicture.edit @op.id, @other_artist
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない原画を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          r = OriginalPicture.edit 0, @artist
+        }.should raise_error(ActiveRecord::RecordNotFound)
       end
     end
   end
+  describe '単体取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = OriginalPicture.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '2つの項目を含んでいる' do
+      r = OriginalPicture.show_opt[:include]
+      r.should have(2).items
+    end
+    it '素材を含んでいる' do
+      r = OriginalPicture.show_opt[:include]
+      r.has_key?(:resource_picture).should be_true
+    end
+    it '実素材を含んでいる' do
+      r = OriginalPicture.show_opt[:include]
+      r.has_key?(:pictures).should be_true
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      @sbt = FactoryGirl.create :speech_balloon_template
+      @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
+    end
+    it '素材を含んでいる' do
+      r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('resource_picture').should be_true
+    end
+    it '実素材を含んでいる' do
+      r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('pictures').should be_true
+    end
+  end
   describe '作成・更新に於いて' do
     before do
-      @op = Factory.build :original_picture, :artist_id => @artist.id
-      #RMagickのスタブをおいておく
-      class Mgk ; class Image ; end ; end
-      @filesize = 76543
-      Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
-      Mgk.any_instance.stub(:format).with(any_args).and_return('png')
-      Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
-      Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
-      Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
-      Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
-      #原画ファイル削除だけは必ず成功するものとしておく
-      PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
-      #素材取得は新規作成ケースでテストしておく
-      ResourcePicture.stub(:update_picture).with(any_args).and_return(
-        Factory.build :resource_picture, :artist_id => @artist.id
-      )
+      @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
+      @imager = ImagerTest.load "abc\ndef\nghi"
     end
     context '事前チェック' do
       before do
         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
         #それで外部のメソッド呼び出しだけに着目してテストする。
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
-        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
-      end
-      it 'RMagick変換を依頼している' do
-        OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
-        @op.store 'bindata', @artist, @license.id\r
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
+        PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
       end
       it '自身に属性をセットしている' do
         OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
-        @op.store 'bindata', @artist, @license.id\r
+        @op.store @imager
       end
       it '自身が保存されている' do
-        OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
-        @op.store 'bindata', @artist, @license.id\r
+        OriginalPicture.any_instance.should_receive(:save).exactly(1)
+        @op.store @imager
       end
       it 'PictureIoに画像データの保存を依頼している' do
-        PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
-        @op.store 'bindata', @artist, @license.id\r
-      end
-      it '素材モデルに対象素材を問い合わせている' do
-        ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
-        @op.store 'bindata', @artist, @license.id\r
-      end
-      it '対象素材オブジェクトに保存を依頼している' do
-        ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
-        @op.store 'bindata', @artist, @license.id\r
+        PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
+        @op.store @imager
       end
     end
     context 'つつがなく終わるとき' do
       before do
-        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-#        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
-        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
       end
       it '自身に属性をセットしている' do
-        lambda {
-          @op.store 'bindata', @artist, @license.id\r
-        }.should change @op, :filesize
+        @op.store @imager
+        @op.width.should eq 3
+        @op.height.should eq 3
+        @op.filesize.should eq 9
       end
       it '原画モデルが作成されている' do
         lambda {
-          @op.store 'bindata', @artist, @license.id
+          @op.store @imager
         }.should change OriginalPicture, :count
       end
       it '原画が保存されている' do
-        @op.store 'bindata', @artist, @license.id
-        OriginalPicture.find(@op).should_not be_nil\r
+        @op.store @imager
+        OriginalPicture.find(@op).should_not be_nil
       end
       it 'Trueを返す' do
-        @op.store('bindata', @artist, @license.id).should eq true\r
+        @op.store(@imager).should eq true
       end
     end
     #以下から例外ケース。処理先頭から失敗させていく
-    context 'RMagick変換が失敗したとき' do
+    context 'imagerが初期化に失敗したとき' do
       before do
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
       end
       it 'falseを返す' do
-        @op.store('bindata', @artist, @license.id).should be_false\r
+        @op.store(false).should be_false
       end
       it '自身の保存は呼ばれていない' do
-        OriginalPicture.any_instance.should_not_receive(:save)\r
+        OriginalPicture.any_instance.should_not_receive(:save)
+        @op.store(false)
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @op.store(false)
+        }.should change(@op.errors[:base], :count)
       end
     end
     context '自身の保存に失敗したとき' do
       before do
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)\r
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
       end
       it 'falseを返す' do
-        @op.store('bindata', @artist, @license.id).should be_false\r
+        @op.store(@imager).should be_false
       end
       it '更新されていない' do
-        @op.store('bindata', @artist, @license.id)
-        @op.should be_a_new OriginalPicture\r
+        @op.store(@imager)
+        @op.should be_a_new OriginalPicture
       end
       it '原画の保存は呼ばれていない' do
-        PictureIO::LocalPicture.any_instance.should_not_receive(:put)\r
+        PictureIO::LocalPicture.any_instance.should_not_receive(:put)
       end
     end
     context '原画の保存に失敗したとき' do
       before do
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
+        PictureIO.original_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
       end
       it 'falseを返す' do
-        @op.store('bindata', @artist, @license.id).should be_false\r
+        @op.store(@imager).should be_false
       end
       it '更新されていない' do
-        @op.store('bindata', @artist, @license.id)\r
-        @op.should be_a_new OriginalPicture\r
-      end
-      it '対象素材オブジェクトの保存は呼ばれていない' do
-        ResourcePicture.any_instance.should_not_receive(:save)\r
-        @op.store('bindata', @artist, @license.id)\r
+        @op.store(@imager)
+        @op.should be_a_new OriginalPicture
       end
       it '全体エラーメッセージがセットされている' do
         lambda {
-          @op.store('bindata', @artist, @license.id)\r
+          @op.store(@imager)
         }.should change(@op.errors[:base], :count)
       end
     end
-    context '対象素材オブジェクトの保存に失敗したとき' do
+  end
+  
+  describe '削除に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    context '事前チェックしておく' do
       before do
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
-        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
-        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
-        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
+        OriginalPicture.any_instance.stub(:destroy).and_return(true)
+        ResourcePicture.any_instance.stub(:unpublish).and_return(true)
+        Picture.any_instance.stub(:unpublish).with(any_args).and_return(true)
+        PictureIO.original_picture_io.stub(:delete).with(any_args).and_return(true)
+      end
+      it '原画モデルに削除を依頼している' do
+        OriginalPicture.any_instance.should_receive(:destroy).exactly(1)
+        r = @op.destroy_with_resource_picture
+      end
+      it '保管庫に原画の画像データ削除を依頼している' do
+        PictureIO.original_picture_io.should_receive(:delete).with(@op.filename).exactly(1)
+        r = @op.destroy_with_resource_picture
+      end
+      context '自身にリンクされた素材があるとき' do
+        it '素材モデルに削除を依頼している' do
+          @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
+          @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+          ResourcePicture.any_instance.should_receive(:unpublish).exactly(1)
+          r = @op.destroy_with_resource_picture
+        end
+      end
+      context '自身にリンクされた素材がないとき' do
+        it '素材モデルに削除を依頼しない' do
+          ResourcePicture.any_instance.should_not_receive(:unpublish)
+          r = @op.destroy_with_resource_picture
+        end
+      end
+      context '自身にリンクされた実素材があるとき' do
+        it 'すべての実素材に墨塗を依頼している' do
+          @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
+          Picture.any_instance.should_receive(:unpublish).with(any_args).exactly(1)
+          r = @op.destroy_with_resource_picture
+        end
+      end
+      context '自身にリンクされた実素材がないとき' do
+        it '実素材に墨塗を依頼しない' do
+          Picture.any_instance.should_not_receive(:unpublish)
+          r = @op.destroy_with_resource_picture
+        end
       end
-      it 'falseを返す' do
-        @op.store('bindata', @artist, @license.id).should be_false\r
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        PictureIO.original_picture_io.stub(:delete).with(any_args).and_return(true)
+        @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
       end
-      it '更新されていない' do
-        @op.store('bindata', @artist, @license.id)\r
-        @op.should be_a_new OriginalPicture\r
+      it '自身を削除する' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should change(OriginalPicture, :count).by(-1)
+        lambda {
+          r = OriginalPicture.find @op.id
+        }.should raise_error
       end
-      it '全体エラーメッセージがセットされている' do
+      it 'Trueを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_true
+      end
+    end
+    context '自身の削除に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:destroy).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
+      end
+      it 'ロールバックしている' do
         lambda {
-          @op.store('bindata', @artist, @license.id)\r
-        }.should change(@op.errors[:base], :count)
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
+      end
+    end
+    context '画像の削除に失敗したとき' do
+      before do
+        PictureIO.original_picture_io.stub(:exist?).with(@op.filename).and_return(true)
+        PictureIO.original_picture_io.stub(:delete).with(@op.filename).and_raise(PictureIO::Error)
+      end
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
+      end
+      it 'ロールバックしている' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
+      end
+    end
+    context '素材の削除に失敗とき' do
+      before do
+        @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+        ResourcePicture.any_instance.stub(:unpublish).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
+      end
+      it 'ロールバックしている' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(ResourcePicture, :count)
+      end
+    end
+    context '実素材の墨塗に失敗とき' do
+      before do
+        @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :revision => 0
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+        Picture.any_instance.stub(:unpublish).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
+      end
+      it 'ロールバックしている' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(ResourcePicture, :count)
+      end
+    end
+  end
+  
+  describe 'エクスポートに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      #開始日時以前の原画
+      @old_op = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now - 1000
+      #素材がライセンスされていない原画
+      @stopped_op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      #他人の原画
+      @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      #貸手からの原画排除
+      @outer_artist = FactoryGirl.create :artist_yas, :author_id => nil
+      @outer_op = FactoryGirl.create :original_picture, :artist_id => @outer_artist.id
+    end
+    context 'つつがなく終わるとき' do
+      it '開始日時が省略された場合はすべての内原画を返す' do
+        r = OriginalPicture.export 
+        r.should eq [@op, @old_op, @stopped_op, @other_op]
+      end
+      it '開始日時以降に更新された内原画を返す' do
+        r = OriginalPicture.export @op.updated_at - 100
+        r.should eq [@op, @stopped_op, @other_op]
+      end
+      it '素材を含んでいる' do
+        r = OriginalPicture.export 
+        r.first.resource_picture.should_not be_nil
+      end
+      #素材がライセンスされていないケースもある
+      it '停止中の原画は素材を含んでいない' do
+        r = OriginalPicture.export
+        r[2].resource_picture.should be_nil
       end
     end
   end
+  
+  describe 'リストのjson化に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
+      @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100
+      @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id
+      ResourcePicture.any_instance.stub(:restore).with(any_args).and_return('picture binary data')
+    end
+    context 'つつがなく終わるとき' do
+      it 'json文字列を返す' do
+        r = OriginalPicture.list_as_json_text OriginalPicture.all 
+        lambda {
+          j = JSON.parse r
+        }.should_not raise_error(JSON::ParserError)
+      end
+      it '原画リストを返す' do
+        r = OriginalPicture.list_as_json_text OriginalPicture.all 
+        j = JSON.parse r
+        j.size.should eq 2
+      end
+      it '素材を含んでいる' do
+        r = OriginalPicture.list_as_json_text OriginalPicture.all 
+        j = JSON.parse r
+        j.first['resource_picture'].should_not be_nil
+      end
+      it '各素材に画像データを添えて返す' do
+        r = OriginalPicture.list_as_json_text OriginalPicture.all 
+        j = JSON.parse r
+        j.first['resource_picture']['picture_data'].should_not be_nil
+        j.last['resource_picture']['picture_data'].should_not be_nil
+      end
+    end
+  end
+  
+  describe 'インポートに於いて' do
+    before do
+      @imports = {:licenses => {}, :artist_id => @artist.id}
+    end
+    context '事前チェックしておく' do
+    end
+  end
+  
 end