require File.dirname(__FILE__) + '/../test_helper' require File.dirname(__FILE__) + '/../test_helpers/cache_helper' class Cms::PagesTest < ActionController::IntegrationTest def setup @company = Factory(:company) @user = @company.users.first login_company(@user.username, 'password') set_company_host(@company) stub_paperclip! end context "page title" do should "show the page title from a child template" do layout = Factory(:page, :slug => nil, :published => false, :content => read_fixture('layout.liquid'), :context => @company) page = Factory(:page, :name => 'content', :slug => "/content", :content => "
No title
", :layout_page => layout, :context => @company) get page.url assert_select 'title', 'Company Website' page.update_attribute :content, "{% assign title = 'Page Title' %}Has Title
" get page.url assert_select 'title', 'Company Website - Page Title' page.update_attribute :content, "{% capture title %}{{ 'Captured Page' }} {{ 'Title' }}{% endcapture %}Has Title
" get page.url assert_select 'title', 'Company Website - Captured Page Title' end end context "page loading" do context "root page" do should "load" do root = @company.pages.root assert_not_nil root get '/' assert_response :success assert_equal assigns(:page), root assert_equal 'This is the home page', response.body get '' assert_response :success assert_equal assigns(:page), root assert_equal 'This is the home page', response.body end should "render the first found published page if no root exists" do page = @company.pages.root page.update_attribute :root, false assert_nil @company.pages.root get '/' assert_response :success assert_equal assigns(:page), page get '' assert_response :success assert_equal assigns(:page), page end should "return an error if no root or published pages exist" do @company.pages.update_all :published => false, :root => false assert_nil @company.pages.root assert @company.pages.published.empty? get '/' assert_response 404 get '' assert_response 404 end should "load a cms page" do get 'home_page' assert_response :success assert_equal 'text/html', @response.content_type assert_equal 'This is the home page', response.body # additional values can be added to a url to be used for more dyanmic pages get 'home_page/123' assert_response :success assert_equal 'text/html', @response.content_type assert_equal 'This is the home page', response.body # if we create a new page with the previous slug, it gets called instead @company.pages.create :name => 'another_hom', :slug => '/home_page/123', :content => 'Different Home', :published => true get 'home_page/123' assert_response :success assert_equal 'Different Home', response.body end end context "drops" do context "params" do should "not allow access to :controller and :action values" do @page = @company.pages.root @page.update_attribute :content, %Q({{ params.action }} {{ params.controller }} {{ params.test }}) get '/?test=5' assert_select '#test', :text => '5' # action and controller aren't exposed assert_select '#action', :text => 'load', :count => 0 assert_select '#controller', :text => 'pages', :count => 0 end end end context "tags" do context "include" do should "show the contents of the included page" do @company.pages.create :name => 'content', :content => %Q(test this string
', response.body @page.update_attribute :content, %Q({% capture text %}test *this* string{% endcapture %}{{ text | textilize: false }}) get @page.url assert_equal 'test this string', response.body end end context "image_tag" do should "show a title" do @page.update_attribute :content, %Q({{ 'test.jpg' | image_tag: "title" }}) get @page.url assert_equal '', response.body end should "show a title and size" do @page.update_attribute :content, %Q({{ 'test.jpg' | image_tag: "title", "23x45" }}) get @page.url assert_equal '', response.body end should "show just a size" do @page.update_attribute :content, %Q({{ 'test.jpg' | image_tag: nil, "23x45" }}) get @page.url assert_equal '', response.body end should "render nothing" do @page.update_attribute :content, %Q({{ nil | image_tag: nil, "23x45" }}) get @page.url assert_equal '', response.body end end context "url_encode and url_decode" do should "properly escape and unescape data" do @page.update_attributes :slug => '/path', :content => %Q({{ 'test spaces' | url_encode }}{{ params.url[0] }}{{ params.url[1] | url_decode }}) url = "#{@page.url}/#{CGI::escape 'test spaces'}" assert_equal url, "/path/test+spaces" get url assert_select '#test', 'test+spaces' assert_select '#first', 'path' assert_select '#second', 'test spaces' end should "handle an empty value" do @page.update_attributes :slug => '/path', :content => %Q({{ nil | url_encode }}) get @page.url assert_select '#test', '' end end context "liquified json data" do should "be valid for users" do @company.pages.create :name => 'test', :published => true, :content => "{% user_data %} {{ users | json }}" get '/test' # decode the json to access the data data = ActiveSupport::JSON.decode(@response.body) assert_equal data.length, @company.users.length assert_equal data[0]['user']['username'], 'user' assert_equal data[0]['user']['company_id'], @company.id end end end should "load a stylesheet page" do @company.pages.create :name => 'test.css', :content => 'body { color: white; }' get "/cms/stylesheets/test.css" assert_response :success assert_equal 'text/css', @response.content_type assert_equal 'body { color: white; }', @response.body end should "throw an exception if a page isn't found" do get '/home_page_not_found' assert_response 404 end should "not load reserved paths for namespaced routes" do get '/support' assert_response 404 end should "not load an unknown path for reserved namespaces" do get '/cms/pages/unknown' assert_response 404 end end context "caching" do setup do ActionController::Base.perform_caching = true Rails.cache.clear @page = Factory(:page, :context => @company) end teardown do ActionController::Base.perform_caching = false end =begin should "expire the cache when the page is updated" do assert_cache_empty get @page.url assert_cache_present # updating the page will remove the cache put cms_page_path(@page), :content => 'new content' assert_cache_empty get @page.url assert_cache_present # destroying the page will remove the cache delete cms_page_path(@page) assert_cache_empty end should "generate a cache key" do get '/' assert_cache_key "views/CONTEXT_PATH_#{@company.id}" get @page.url assert_cache_key "views/CONTEXT_PATH_#{@company.id}/page" get @page.url+'?page=1&test=abcde' assert_cache_key "views/CONTEXT_PATH_#{@company.id}/page/page=1&test=abcde" # different order sent via url, but the path params will be sorted in the path get @page.url+'?test=abcde&page=1' assert_cache_key "views/CONTEXT_PATH_#{@company.id}/page/page=1&test=abcde" end =end end end