OSDN Git Service

t30350#:fix destroy in op, p, user
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
index 73b1f80..8991830 100644 (file)
@@ -4,12 +4,12 @@ require 'spec_helper'
 
 describe OriginalPicture do
   before do
-    FactoryGirl.create :admin
+    @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
@@ -155,14 +155,28 @@ describe OriginalPicture 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 '自作の原画ならyes' do
+        @op.own?(@author).should == true
+      end
+      it '他人のならno' do
+        @op.own?(@other_author).should == false
+      end
+    end
+    context 'パラメータが絵師のとき' do
+      it '自作の原画ならyes' do
+        @op.own?(@artist).should == true
+      end
+      it '他人のならno' do
+        @op.own?(@other_artist).should == false
+      end
     end
-    it '他人のならno' do
-      op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
-      op.own?(@artist).should == false
+    context 'それ以外のとき' do
+      it 'no' do
+        @op.own?(nil).should == false
+      end
     end
   end
   
@@ -170,7 +184,12 @@ describe OriginalPicture do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
-    it '自分の原画を見るときは許可する' do
+    it '検査対象が管理者のとき、許可を返す' do
+      OriginalPicture.any_instance.stub(:own?).and_return(false)
+      r = @op.visible?(@admin)
+      r.should == true
+    end
+    it '所持判定を問い合わせ、自分の原画なら許可する' do
       OriginalPicture.any_instance.stub(:own?).and_return(true)
       r = @op.visible?(@artist)
       r.should == true
@@ -217,7 +236,7 @@ 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')
@@ -225,7 +244,33 @@ describe OriginalPicture do
     end
     it 'サムネイル画像の幅高さ取得を依頼している' do
       PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
-      @op.opt_img_tag
+      @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 +284,131 @@ 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
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
+        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
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
+        r = @op.stopped?
+        r.should be_true
+      end
+    end
+    context '未公開のとき' do
+      it 'Falseを返す' do
+        r = @op.stopped?
+        r.should be_false
+      end
+    end
+    context '未公開ではなく、素材もあるとき' do
+      it 'falseを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+        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 '未公開ではなく、素材もあるが、原画の更新日時が実素材のheadのそれより後のとき' do
+      it 'Trueを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now - 1000
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+        r = @op.unlicensed?
+        r.should be_true
+      end
+    end
+    context '未公開のとき' do
+      it 'Falseを返す' do
+        r = @op.unlicensed?
+        r.should be_false
+      end
+    end
+    context '未公開ではないが、素材がないとき' do
+      it 'Falseを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
+        r = @op.unlicensed?
+        r.should be_false
+      end
+    end
+    context '未公開ではなく、素材もあり、実素材のheadの更新日時が原画のそれより後のとき' do
+      it 'Falseを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now + 1000
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+        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 '未公開ではなく、素材もあり、実素材のheadの更新日時が原画のそれより後のとき' do
+      it 'Trueを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now + 1000
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+        r = @op.published?
+        r.should be_true
+      end
+    end
+    context '未公開のとき' do
+      it 'Falseを返す' do
+        r = @op.published?
+        r.should be_false
+      end
+    end
+    context '未公開ではないが、素材がないとき' do
+      it 'falseを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
+        r = @op.published?
+        r.should be_false
+      end
+    end
+    context '未公開ではなく、素材もあるが、原画の更新日時が実素材のheadのそれより後のとき' do
+      it 'falseを返す' do
+        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now - 1000
+        @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+        r = @op.published?
+        r.should be_false
+      end
+    end
+  end
+
   describe '一覧取得に於いて' do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
@@ -278,7 +439,7 @@ describe OriginalPicture do
       end
     end
   end
-  describe 'list関連テーブルプションに於いて' do
+  describe '一覧取得オプションに於いて' do
     it 'includeキーを含んでいる' do
       r = OriginalPicture.list_opt
       r.has_key?(:include).should be_true
@@ -297,25 +458,30 @@ describe OriginalPicture do
     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
+    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
+      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
     end
     it '素材を含んでいる' do
-      r = OriginalPicture.list_json_opt[:include]
-      r.has_key?(:resource_picture).should be_true
+      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.list_json_opt[:include]
-      r.has_key?(:pictures).should be_true
+      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
+  describe '自分の原画一覧取得に於いて' do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
     end
@@ -335,16 +501,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
@@ -376,6 +537,7 @@ describe OriginalPicture do
         @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
+        OriginalPicture.stub(:default_page_size).and_return(2)
       end
       it '通常は全件(5件)を返す' do
         r = OriginalPicture.mylist @artist, 5, 0
@@ -481,7 +643,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 +662,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
+      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.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 +766,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,6 +783,140 @@ describe OriginalPicture do
     end
   end
   
+  describe '削除に於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    end
+    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
+    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
+        lambda {
+          r = @op.destroy_with_resource_picture
+        }.should change(OriginalPicture, :count).by(-1)
+        lambda {
+          r = OriginalPicture.find @op.id
+        }.should raise_error
+      end
+      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 {
+          r = @op.destroy_with_resource_picture
+        }.should_not change(OriginalPicture, :count)
+      end
+    end
+    context '画像の削除に失敗したとき' do
+      before do
+        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
+  
 =begin
   describe 'エクスポートに於いて' do
     before do