OSDN Git Service

t#30102#30122:update i18n pictures
[pettanr/pettanr.git] / spec / models / picture_spec.rb
index 45af380..eb6e962 100644 (file)
@@ -203,7 +203,7 @@ describe Picture do
   
   describe 'デフォルト値補充に於いて' do
     it 'defined' do
-      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
       @p.supply_default
     end
   end
@@ -214,7 +214,7 @@ describe Picture do
         :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, 
         :artist_name => 'tester', :classname => 'Tester', :credit => {:title => 'cap'}.to_json.to_s, :settings => {:set => 1}.to_json.to_s}
       @rp = FactoryGirl.build :resource_picture, attr
-      @p = FactoryGirl.build :picture, :original_picture_id => nil, :license_id => nil, :artist_id => nil, :revision => nil\r
+      @p = FactoryGirl.build :picture, :original_picture_id => nil, :license_id => nil, :artist_id => nil, :revision => nil
     end
     it 'width, height, ext, filesize, md5, original_picture_idが設定されている' do
       @p.overwrite @rp
@@ -248,26 +248,76 @@ describe Picture do
   
   describe '所持判定に於いて' do
     before do
-      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
     end
-    it '実素材を更新することはないので、Falseを返す' do
-      @p.own?(@author).should == false
+    context 'パラメータが作家のとき' do
+      it '自作の実素材ならyes' do
+        @p.own?(@author).should == true
+      end
+      it '他人のならno' do
+        @p.own?(@other_author).should == false
+      end
+    end
+    context 'パラメータが絵師のとき' do
+      it '自作の実素材ならyes' do
+        @p.own?(@artist).should == true
+      end
+      it '他人のならno' do
+        @p.own?(@other_artist).should == false
+      end
+    end
+    context 'それ以外のとき' do
+      it 'no' do
+        @p.own?(nil).should == false
+      end
     end
   end
-  
   describe '閲覧許可に於いて' do
     before do
-      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+    end
+    it 'Trueを返す。' do
+      r = @p.visible?(@artist)
+      r.should be_true
+    end
+  end
+  
+  describe '詳細閲覧許可に於いて' do
+    before do
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+    end
+    it '自作の実素材ならyes' do
+      Picture.any_instance.stub(:own?).with(any_args).and_return(true)
+      @p.showable?(@artist).should == true
+    end
+    context '他人の実素材のとき' do
+      before do
+        Picture.any_instance.stub(:own?).with(any_args).and_return(false)
+      end
+      it '自身にhead判定と有効性判定を問い合わせ、両者がTrueならTrueを返す。' do
+        Picture.any_instance.stub(:head?).with(any_args).and_return(true)
+        Picture.any_instance.stub(:enable?).with(any_args).and_return(true)
+        r = @p.showable?(@artist)
+        r.should be_true
+      end
+      it 'head判定がFalseならFalseを返す。' do
+        Picture.any_instance.stub(:head?).with(any_args).and_return(false)
+        Picture.any_instance.stub(:enable?).with(any_args).and_return(true)
+        r = @p.showable?(@artist)
+        r.should be_false
+      end
+      it '有効性判定がFalseならFalseを返す。' do
+        Picture.any_instance.stub(:enable?).with(any_args).and_return(false)
+        Picture.any_instance.stub(:head?).with(any_args).and_return(true)
+        r = @p.showable?(@artist)
+        r.should be_false
+      end
     end
-    it '必ず許可となる' do\r
-      r = @p.visible?(@author)
-      r.should == true
-    end\r
   end
   
   describe 'ファイル名に於いて' do
     before do
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
     end
     it 'id+拡張子のフォーマットで返す' do
       r = @p.filename
@@ -277,7 +327,7 @@ describe Picture do
   
   describe 'gifファイル名に於いて' do
     before do
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'gif'\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'gif'
     end
     it 'id.gifのフォーマットで返す' do
       r = @p.filename
@@ -287,7 +337,7 @@ describe Picture do
   
   describe 'MimeTypeに於いて' do
     before do
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
     end
     it 'image/拡張子のフォーマットで返す' do
       r = @p.mime_type
@@ -297,7 +347,7 @@ describe Picture do
   
   describe 'ファイルのurlに於いて' do
     before do
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
       Picture.any_instance.stub(:filename).and_return('3.gif')
     end
     it 'ファイル名取得を依頼している' do
@@ -340,89 +390,146 @@ describe Picture do
     end
   end
   
-  describe 'head判定に於いて' do
+  describe '有効性判定に於いて' do
     before 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
+      Picture.any_instance.stub(:head).and_return(@p)
     end
-    context '自身とリンクした素材があるとき' do
+    context 'è\87ªèº«ã\81®headã\81¨ã\83ªã\83³ã\82¯ã\81\97ã\81\9fç´ æ\9d\90ã\81\8cã\81\82ã\82\8bã\81¨ã\81\8d' do
       before 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
+        Picture.any_instance.stub(:resource_picture).and_return(@rp)
       end
       it 'trueを返す' do
-        res = @p.head?
+        res = @p.enable?
         res.should be_true
       end
     end
-    context '自身とリンクした素材がないとき' do
+    context 'è\87ªèº«ã\81®headã\81¨ã\83ªã\83³ã\82¯ã\81\97ã\81\9fç´ æ\9d\90ã\81\8cã\81ªã\81\84ã\81¨ã\81\8d' do
       before do
-        @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
-        @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 1
+        Picture.any_instance.stub(:resource_picture).and_return(nil)
+      end
+      it 'falseを返す' do
+        res = @p.enable?
+        res.should be_false
+      end
+    end
+  end
+  
+  describe 'head取得に於いて' 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, :md5 => 'a' * 32
+      #最新版
+      @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
+        :original_picture_id => @op.id, :md5 => 'b' * 32
+      #除外すべき無関係画像
+      @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
+        :original_picture_id => @op2.id, :md5 => 'C' * 32
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        #素材は有効
         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p2.id
       end
+      it '最新版を返す' do
+        res = @p.head
+        res.should eq @p2
+      end
+    end
+    context '無効な素材(素材とリンクしてない)とき' do
+      it '同じく最新版を返す' do
+        res = @p.head
+        res.should eq @p2
+      end
+    end
+  end
+  
+  describe 'head判定に於いて' 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, :md5 => 'a' * 32
+      #最新版
+      @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
+        :original_picture_id => @op.id, :md5 => 'b' * 32
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p2.id
+    end
+    context '旧版のとき' do
       it 'falseを返す' do
         res = @p.head?
         res.should be_false
       end
     end
+    context '最新版のとき' do
+      it 'trueを返す' do
+        res = @p2.head?
+        res.should be_true
+      end
+    end
   end
   
-  describe 'フォーマット変換対象判定に於いて' do\r
-    before do\r
+  describe 'フォーマット変換対象判定に於いて' do
+    before do
       @p = FactoryGirl.build :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id
-    end\r
-    context '変換するケース' do\r
-      it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
-        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
-        @p.ext = 'png'\r
-        @p.to_gif?.should be_true\r
-      end\r
-    end\r
-    context '変換しないケース' do\r
-      it '画像フォーマットがpngでない' do\r
-        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
-        @p.ext = 'gif'\r
-        @p.to_gif?.should be_false\r
-      end\r
-      it '変換禁止フラグが無効' do\r
-        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)\r
-        @p.ext = 'png'\r
-        @p.to_gif?.should be_false\r
-      end\r
-    end\r
-  end\r
-  \r
-  describe 'サブディレクトリリストに於いて' do\r
-    before do\r
+    end
+    context '変換するケース' do
+      it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do
+        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
+        @p.ext = 'png'
+        @p.to_gif?.should be_true
+      end
+    end
+    context '変換しないケース' do
+      it '画像フォーマットがpngでない' do
+        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
+        @p.ext = 'gif'
+        @p.to_gif?.should be_false
+      end
+      it '変換禁止フラグが無効' do
+        Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)
+        @p.ext = 'png'
+        @p.to_gif?.should be_false
+      end
+    end
+  end
+  
+  describe 'サブディレクトリリストに於いて' do
+    before do
       @p = FactoryGirl.build :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id
-    end\r
-    it '配列で返す' do\r
-      Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
-      r = @p.subdirs\r
-      r.is_a?(Array).should be_true\r
-    end\r
-    it '本画像(ベースディレクトリ)を含んでいる' do\r
-      Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)\r
-      r = @p.subdirs\r
-      r.include?('').should be_true\r
-    end\r
-    context '反転が許可されているとき' do\r
-      it '垂直・水平・垂直水平反転ディレクトリも返す' do\r
-        Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)\r
-        r = @p.subdirs\r
-        r.include?('v').should be_true\r
-        r.include?('h').should be_true\r
-        r.include?('vh').should be_true\r
-      end\r
-    end\r
-    context '反転が許可されていないとき' do\r
-      it '本画像(ベースディレクトリ)だけを返す' do\r
-        Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)\r
-        r = @p.subdirs\r
-        r.size.should eq 1\r
-      end\r
-    end\r
-  end\r
-  \r
+    end
+    it '配列で返す' do
+      Picture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
+      r = @p.subdirs
+      r.is_a?(Array).should be_true
+    end
+    it '本画像(ベースディレクトリ)を含んでいる' do
+      Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
+      r = @p.subdirs
+      r.include?('').should be_true
+    end
+    context '反転が許可されているとき' do
+      it '垂直・水平・垂直水平反転ディレクトリも返す' do
+        Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
+        r = @p.subdirs
+        r.include?('v').should be_true
+        r.include?('h').should be_true
+        r.include?('vh').should be_true
+      end
+    end
+    context '反転が許可されていないとき' do
+      it '本画像(ベースディレクトリ)だけを返す' do
+        Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
+        r = @p.subdirs
+        r.size.should eq 1
+      end
+    end
+  end
+  
   describe 'md5検索に於いて' do
     before do
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
@@ -538,7 +645,7 @@ describe Picture do
   
   describe '単体取得に於いて' do
     before do
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
     end
     context 'つつがなく終わるとき' do
       it '閲覧許可を問い合わせている' do
@@ -581,7 +688,7 @@ describe Picture do
         Picture.any_instance.stub(:save).with(any_args).and_return(true)
         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it '自身を保存している' do
         Picture.any_instance.should_receive(:save).with(any_args).exactly(1)
@@ -603,7 +710,7 @@ describe Picture do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Trueを返す' do
         r = @p.store(@imager)
@@ -620,7 +727,7 @@ describe Picture do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(false)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Trueを返す' do
         r = @p.store(@imager)
@@ -636,7 +743,7 @@ describe Picture do
     context '自身の保存に失敗したとき' do
       before do
         Picture.any_instance.stub(:save).with(any_args).and_return(false)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Falseを返す' do
         r = @p.store(@imager)
@@ -650,7 +757,7 @@ describe Picture do
     context '画像の保存に失敗したとき' do
       before do
         Picture.any_instance.stub(:store_picture).with(any_args).and_return(false)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Falseを返す' do
         r = @p.store(@imager)
@@ -666,7 +773,7 @@ describe Picture do
         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
         ImagerTest.stub(:load).with(any_args).and_return(false)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Falseを返す' do
         r = @p.store(@imager)
@@ -687,7 +794,7 @@ describe Picture do
         Picture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)
         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
         Picture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)
-        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+        @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
       end
       it 'Falseを返す' do
         r = @p.store(@imager)
@@ -704,7 +811,7 @@ describe Picture do
       PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
       
       Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])
-      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
+      @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'
     end
     context '事前チェック' do
       before do