OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
index 7c82cbf..ede81d9 100644 (file)
@@ -4,12 +4,13 @@ require 'spec_helper'
 
 describe OriginalPicture do
   before do
-    FactoryGirl.create :admin
+    SpeechBalloonTemplate.delete_all
+    @admin = FactoryGirl.create :admin
     @user = FactoryGirl.create( :user_yas)
-    @author = @user.author
+    @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 = @other_user.author
+    @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
@@ -146,6 +147,11 @@ describe OriginalPicture do
   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
@@ -153,16 +159,37 @@ describe OriginalPicture do
     end
   end
   
-  describe '作者判定に於いて' do
+  describe '所持判定に於いて' do
     before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
-    it '自作の原画ならyes' do
-      op = FactoryGirl.create :original_picture, :artist_id => @artist.id
-      op.own?(@artist).should == true
+    context '事前チェックする' do
+      it '自身にロールリストからの絵師取得を依頼している' do
+        OriginalPicture.should_receive(:get_artist_from_roles).with(any_args).exactly(1)
+        r = @op.own?([@artist])
+      end
     end
-    it '他人のならno' do
-      op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
-      op.own?(@artist).should == false
+    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
   
@@ -170,15 +197,30 @@ describe OriginalPicture do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
-    it '自分の原画を見るときは許可する' do
-      OriginalPicture.any_instance.stub(:own?).and_return(true)
-      r = @op.visible?(@artist)
-      r.should == true
+    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 '他人の原画なら許可しない' do
-      OriginalPicture.any_instance.stub(:own?).and_return(false)
-      r = @op.visible?(@artist)
-      r.should == false
+    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
   end
   
@@ -217,15 +259,41 @@ describe OriginalPicture do
     end
   end
   
-  describe '画像タグオプションに於いて' do
+  describe 'サムネイル画像タグオプションに於いて' do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
       OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
-      ResourcePicture.stub(:fix_size_both).with(any_args).and_return([40, 30])
+      PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
     end
     it 'サムネイル画像の幅高さ取得を依頼している' do
-      ResourcePicture.should_receive(:fix_size_both).with(any_args).exactly(1)
-      @op.opt_img_tag
+      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
@@ -239,15 +307,119 @@ describe OriginalPicture do
     it 'widthキーを含んでいる' do
       r = @op.opt_img_tag
       r.has_key?(:width).should be_true
-      r[:width].should eq 40
+      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 30
+      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 '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
+  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 '停止日時が空のとき' do
+      it 'Falseを返す' do
+        @op.stopped_at = nil
+        r = @op.stopped?
+        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.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 = FactoryGirl.create :original_picture, :artist_id => @artist.id
@@ -278,44 +450,8 @@ describe OriginalPicture do
       end
     end
   end
-  describe 'list関連テーブルプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = OriginalPicture.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = OriginalPicture.list_opt[:include]
-      r.should have(2).items
-    end
-    it '素材を含んでいる' do
-      r = OriginalPicture.list_opt[:include]
-      r.has_key?(:resource_picture).should be_true
-    end
-    it '実素材を含んでいる' do
-      r = OriginalPicture.list_opt[:include]
-      r.has_key?(:pictures).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = OriginalPicture.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = OriginalPicture.list_json_opt[:include]
-      r.should have(2).items
-    end
-    it '素材を含んでいる' do
-      r = OriginalPicture.list_json_opt[:include]
-      r.has_key?(:resource_picture).should be_true
-    end
-    it '実素材を含んでいる' do
-      r = OriginalPicture.list_json_opt[:include]
-      r.has_key?(:pictures).should be_true
-    end
-  end
   
-  describe '自分のコミック一覧取得に於いて' do
+  describe '自分の原画一覧取得に於いて' do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
@@ -335,16 +471,11 @@ describe OriginalPicture do
       r = OriginalPicture.mylist @artist
       r.should eq [nc, @op]
     end
-    it '他人のコミックはxxxでも含まない' do
+    it '他人の原画は含まない' do
       nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
       r = OriginalPicture.mylist @artist
       r.should eq [@op]
     end
-    it '自分のコミックはxxxでも含んでいる' do
-      nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
-      r = OriginalPicture.mylist @artist
-      r.should eq [nc, @op]
-    end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
@@ -370,17 +501,63 @@ describe OriginalPicture do
         r.should eq [@op]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @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 '通常は全件(5件)を返す' do
-        r = OriginalPicture.mylist @artist, 5, 0
-        r.should have(5).items 
-      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
+    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
   
@@ -481,7 +658,7 @@ describe OriginalPicture do
       end
     end
   end
-  describe 'å\8d\98ä½\93å\87ºå\8a\9bオプションに於いて' do
+  describe 'å\8d\98ä½\93å\8f\96å¾\97オプションに於いて' do
     it 'includeキーを含んでいる' do
       r = OriginalPicture.show_opt
       r.has_key?(:include).should be_true
@@ -500,21 +677,26 @@ describe OriginalPicture do
     end
   end
   describe 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = OriginalPicture.show_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = OriginalPicture.show_json_opt[:include]
-      r.should have(2).items
+    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_json_opt[:include]
-      r.has_key?(:resource_picture).should be_true
+      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_json_opt[:include]
-      r.has_key?(:pictures).should be_true
+      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
@@ -599,7 +781,7 @@ describe OriginalPicture do
     context '原画の保存に失敗したとき' do
       before do
         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
-        PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
+        PictureIO.original_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
       end
       it 'falseを返す' do
         @op.store(@imager).should be_false
@@ -616,124 +798,211 @@ describe OriginalPicture do
     end
   end
   
-=begin
-  describe 'エクスポートに於いて' do
+  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
-      #他人の原画排除
-      @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
     end
-    context 'つつがなく終わるとき' do
-      it 'ライセンスグループに依頼してリストを取得している' do
-        LicenseGroup.stub(:list).with(any_args).and_return([@lg])
-        LicenseGroup.should_receive(:list).with(any_args).exactly(1)
-        r = OriginalPicture.export(@artist)
+    context '事前チェックしておく' do
+      before do
+        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 '原画に依頼してリストを取得している' do
-        OriginalPicture.stub(:list).with(any_args).and_return([@op])
-        OriginalPicture.should_receive(:list).with(any_args).exactly(1)
-        r = OriginalPicture.export(@artist)
+    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 'Hashを返す' do
-        r = OriginalPicture.export(@artist)
-        r.is_a?(Hash).should be_true
+      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 'Hashはライセンスグループを含んでいる' do
-        r = OriginalPicture.export(@artist)
-        r.include?(:license_groups).should be_true
+      it 'Trueを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_true
       end
-      it 'Hashは原画を含んでいる' do
-        r = OriginalPicture.export(@artist)
-        r.include?(:original_pictures).should be_true
+    end
+    context '自身の削除に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:destroy).with(any_args).and_return(false)
       end
-      #素材がライセンスされていないケースもある
-      it 'Hashの原画は素材を含んでいる' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].first.resource_picture.should_not be_nil
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
       end
-      it 'Hashの原画は実素材を含んでいる' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].first.pictures.should be_nil
+      it 'ロールバックしている' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
       end
     end
-    context '実データ単体のとき' do
-      it 'ライセンスは配列構造になっている' do
-        r = OriginalPicture.export(@artist)
-        r[:license_groups].is_a?(Array).should be_true
+    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 'ライセンスが全件出ている' do
-        r = OriginalPicture.export(@artist)
-        r[:license_groups].size.should eq 1
-        r[:license_groups].first.should eq @lg
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
       end
-      it '原画は配列構造になっている' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].is_a?(Array).should be_true
+      it 'ロールバックしている' do
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
       end
-      it '原画が全件出ている' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].size.should eq 1
-        r[:original_pictures].first.should eq @op
+    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 '原画に素材が関連付いている' do
-        r = OriginalPicture.export(@artist)
-        i = r[:original_pictures].first
-        i.resource_picture.should eq @rp
+      it 'Falseを返す' do
+        r = @op.destroy_with_resource_picture
+        r.should be_false
       end
-      it '原画に実素材が関連付いている' do
-        r = OriginalPicture.export(@artist)
-        i = r[:original_pictures].first
-        i.picture.should eq @p
+      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
+    context '実素材の墨塗に失敗とき' do
       before do
-        @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
-        @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
-        @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
-        @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
-        @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
-      end
-      it 'ライセンスは配列構造になっている' do
-        r = OriginalPicture.export(@artist)
-        r[:license_groups].is_a?(Array).should be_true
-      end
-      it 'ライセンスが全件出ている' do
-        r = OriginalPicture.export(@artist)
-        r[:license_groups].size.should eq 2
-        r[:license_groups].first.should eq @lg
-        r[:license_groups].last.should eq @lg2
-      end
-      it '原画は配列構造になっている' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].is_a?(Array).should be_true
-      end
-      it '原画が全件出ている' do
-        r = OriginalPicture.export(@artist)
-        r[:original_pictures].size.should eq 2
-        r[:original_pictures].first.should eq @op
-        r[:original_pictures].last.should eq @op2
-      end
-      it '原画に素材が関連付いている' do
-        r = OriginalPicture.export(@artist)
-        i = r[:original_pictures].first
-        i.resource_picture.should eq @rp
-        i2 = r[:original_pictures].last
-        i2.resource_picture.should eq @rp2
-      end
-      it '原画に実素材が関連付いている' do
-        r = OriginalPicture.export(@artist)
-        i = r[:original_pictures].first
-        i.picture.should eq @p
-        i2 = r[:original_pictures].last
-        i2.picture.should eq @p2
+        @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 'エクスポートオプションに於いて' do
+  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
@@ -744,5 +1013,4 @@ describe OriginalPicture do
     end
   end
   
-=end
 end