require 'spec_helper' require 'cooked_post_processor' describe CookedPostProcessor do context "post_process" do let(:post) { build(:post) } let(:cpp) { CookedPostProcessor.new(post) } let(:post_process) { sequence("post_process") } it "post process in sequence" do cpp.expects(:post_process_attachments).in_sequence(post_process) cpp.expects(:post_process_images).in_sequence(post_process) cpp.expects(:post_process_oneboxes).in_sequence(post_process) cpp.post_process end end context "post_process_attachments" do context "with attachment" do let(:upload) { Fabricate(:upload) } let(:post) { Fabricate(:post_with_an_attachment) } let(:cpp) { CookedPostProcessor.new(post) } # all in one test to speed things up it "works" do Upload.expects(:get_from_url).returns(upload) cpp.post_process_attachments # ensures absolute urls on attachment cpp.html.should =~ /#{LocalStore.base_url}/ # ensure name is present cpp.html.should =~ /archive.zip/ # keeps the reverse index up to date post.uploads.reload post.uploads.count.should == 1 end end end context "post_process_images" do context "with images in quotes and oneboxes" do let(:post) { build(:post_with_images_in_quote_and_onebox) } let(:cpp) { CookedPostProcessor.new(post) } before { cpp.post_process_images } it "does not process them" do cpp.html.should match_html post.cooked cpp.should_not be_dirty end it "has no topic image if there isn't one in the post" do post.topic.image_url.should be_blank end end context "with locally uploaded images" do let(:upload) { Fabricate(:upload) } let(:post) { Fabricate(:post_with_uploaded_images) } let(:cpp) { CookedPostProcessor.new(post) } before { FastImage.stubs(:size) } # all in one test to speed things up it "works" do Upload.expects(:get_from_url).returns(upload).twice cpp.post_process_images # ensures absolute urls on uploaded images cpp.html.should =~ /#{LocalStore.base_url}/ # dirty cpp.should be_dirty # keeps the reverse index up to date post.uploads.reload post.uploads.count.should == 1 end end context "with sized images" do let(:post) { build(:post_with_image_url) } let(:cpp) { CookedPostProcessor.new(post, image_sizes: {'http://foo.bar/image.png' => {'width' => 111, 'height' => 222}}) } before { FastImage.stubs(:size).returns([150, 250]) } it "doesn't call image_dimensions because it knows the size" do cpp.expects(:image_dimensions).never cpp.post_process_images end it "adds the width from the image sizes provided" do cpp.post_process_images cpp.html.should =~ /width=\"111\"/ cpp.should be_dirty end end context "with unsized images" do let(:post) { build(:post_with_unsized_images) } let(:cpp) { CookedPostProcessor.new(post) } it "adds the width and height to images that don't have them" do FastImage.expects(:size).returns([123, 456]) cpp.post_process_images cpp.html.should =~ /width="123" height="456"/ cpp.should be_dirty end end context "with large images" do let(:upload) { Fabricate(:upload) } let(:post) { build(:post_with_large_image) } let(:cpp) { CookedPostProcessor.new(post) } before do SiteSetting.stubs(:create_thumbnails?).returns(true) Upload.expects(:get_from_url).returns(upload) cpp.stubs(:associate_to_post) FastImage.stubs(:size).returns([1000, 2000]) # optimized_image FileUtils.stubs(:mkdir_p) File.stubs(:open) ImageSorcery.any_instance.expects(:convert).returns(true) end it "generates overlay information" do cpp.post_process_images cpp.html.should match_html '
' cpp.should be_dirty end end context "topic image" do let(:topic) { build(:topic, id: 1) } let(:post) { Fabricate(:post_with_uploaded_images, topic: topic) } let(:cpp) { CookedPostProcessor.new(post) } it "adds a topic image if there's one in the post" do FastImage.stubs(:size).returns([100, 100]) cpp.post_process_images post.topic.reload post.topic.image_url.should == "http://test.localhost/uploads/default/2/3456789012345678.png" end end end context "post_process_oneboxes" do let(:post) { build(:post_with_youtube, id: 123) } let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) } before do Oneboxer.expects(:onebox).with("http://www.youtube.com/watch?v=9bZkp7q19f0", post_id: 123, invalidate_oneboxes: true).returns('