require File.dirname(__FILE__) + '/../test_helper' class Cms::PageTest < ActiveSupport::TestCase def setup ActionController::Base.module_eval do def request OpenStruct.new(:protocol => 'http://', :raw_host_with_port => 'test.com') end end @company = Factory(:company) @controller = ActionController::Base.new end context "data cleaning" do should "clean the slug value" do options = {:content => 'test'} page = @company.pages.create options.merge(:slug => 'test_page1') assert_equal '/test_page1', page.slug page = @company.pages.create options.merge(:slug => '/test_page2/') assert_equal '/test_page2', page.slug page = @company.pages.create options.merge(:slug => '/test_page3//') assert_equal '/test_page3', page.slug page = @company.pages.create options.merge(:slug => '//test_page4//') assert_equal '/test_page4', page.slug end end context "validations" do should "return blank errors" do page = @company.pages.create assert_equal 2, page.errors.length assert_equal "is invalid", page.errors[:name].first assert_equal "can't be blank", page.errors[:content].first page = @company.pages.create :published => true assert_equal 2, page.errors.length assert_equal "is invalid", page.errors[:name].first assert_equal "can't be blank", page.errors[:content].first end should "not allow duplicates" do # same company, duplicate fails page = @company.pages.create :name => @company.pages.first.name, :content => 'Test', :slug => @company.pages.first.slug assert_equal 2, page.errors.length assert_equal "has already been taken", page.errors[:name].first assert_equal "has already been taken", page.errors[:slug].first # multiple blank slugs can be saved page = @company.pages.create :name => 'new_name_1', :content => 'Test' assert page.errors.empty? page = @company.pages.create :name => 'new_name_2', :content => 'Test' assert page.errors.empty? # different company (scope), "duplicate" passes company = Company.create Factory.attributes_for(:company, :name => 'New Company', :domain_name => 'test.com', :subdomain => 'test') page = company.pages.create :name => @company.pages.first.name, :content => 'Test', :slug => @company.pages.first.slug assert_equal 0, page.errors.length end context "name" do should "check valid formats" do page = Factory(:page, :context => @company) page.update_attributes :name => 'test_123' assert page.errors[:name].empty? page.update_attributes :name => 'test_123-test' assert page.errors[:name].empty? page.update_attributes :name => 'test_123-test/test' assert !page.errors[:name].empty? page.update_attributes :name => 'test_123-test.test' assert page.errors[:name].empty? end end context "slugs" do setup do @options = {:name => 'Test', :content => 'Test'} end should "return correct content-types and url" do page = Factory(:page, :slug => nil, :context => @company) page.name = 'test.css' assert_equal 'text/css', page.content_type assert_equal '/cms/stylesheets/test.css', page.url page.name = 'test.js' assert_equal 'text/javascript', page.content_type assert_equal '/cms/javascripts/test.js', page.url page.name = 'test.json' assert_equal 'application/json', page.content_type assert_equal '/test.json', page.url end # should the slug be "cleaned" when assigned to via 'def slug=' should "clean the slug when assigned" =begin assert false page.name = 'test.html' assert_equal 'text/html', page.content_type page.name = 'test.somethingelse' assert_equal 'text/html', page.content_type page.name = 'test' assert_equal 'text/html', page.content_type page.name = 'cssjs' assert_equal 'text/html', page.content_type =end should "allow multiple blank values when not published" do page = @company.pages.create @options.merge(:name => 'test1', :slug => nil, :published => false) assert page.valid? page = @company.pages.create @options.merge(:name => 'test2', :slug => nil, :published => false) assert page.valid? end should "reject any slugs that match namespaced controllers" do page = @company.pages.create @options.merge(:slug => '/cms') assert_equal 1, page.errors.length assert_equal "is reserved and can't be used for this page", page.errors[:slug].first page = @company.pages.create @options.merge(:slug => '/cms') assert_equal 1, page.errors.length assert_equal "is reserved and can't be used for this page", page.errors[:slug].first end should "reject a reserved name" do page = @company.pages.create @options.merge(:slug => '/cms/assets') assert_equal 1, page.errors.length assert_equal "is reserved and can't be used for this page", page.errors[:slug].first end should "confirm reserved paths" do page = @company.pages.create :name => 'test' # reserved names %w(images stylesheets scripts stylesheets/test cms/pages cms/assets admin support cms/pages/test).each do |path| page.update_attributes :slug => "/#{path}" assert page.reserved_slug?, path page.update_attributes :slug => path assert page.reserved_slug?, path end # non-reserved names %w(test/stylesheets/test teststylesheets/test stylesheetstest/test).each do |path| page.update_attributes :slug => "/#{path}" assert !page.reserved_slug? page.update_attributes :slug => path assert !page.reserved_slug? end end end end context "page contents" do should "be identified as a layout file" do page = Factory(:page, :context => @company) page.content = <<-HTML {{ content_for_layout }} HTML assert page.is_layout_page? page.content = <<-HTML {{content_for_layout}} HTML assert page.is_layout_page? page.content = "{{content_for_layout}}" assert page.is_layout_page? page.content = <<-HTML
test
HTML assert !page.is_layout_page? end end context "layout page" do should "not be able to set publish or root flag" do page = Factory.build(:page, :context => @company, :slug => '/testpage', :published => true, :root => true, :content => "{{ content_for_layout }}") page.save %w(slug published root).each do |attr| assert_equal "can't be set for a layout page", page.errors[attr].first end end end context "root flag" do should "only exist for one page" do curr_root = @company.pages.root assert_not_nil curr_root new_root = Factory(:page, :context => @company, :root => true) assert_not_equal curr_root, @company.pages.root assert_equal new_root, @company.pages.root end should "test #verify_single_root" do new_root = Factory(:page, :context => @company, :root => true) assert_equal new_root, @company.pages.root # saving the same page as root should keep the page as root new_root.update_attribute :root, true assert_equal new_root, @company.pages.root end end context "rendering" do should "render the page with no layout" do page = @company.pages.create :name => "Page without layout", :content => 'Test Page' assert_equal 'Test Page', page.rendered_content(@controller) end should "render the page with layout" do outer_layout = @company.pages.create :name => "outer_layout", :content => '{{ content_for_layout }}' inner_layout = @company.pages.create :name => "inner_layout", :content => '