Skip to content
Browse files

Added some tests to pages

  • Loading branch information...
1 parent 770b6a8 commit 6b587c9e6da882394ffacd686b6ba6558571b455 @wlodars wlodars committed Jul 17, 2008
View
4 app/controllers/admin/pages_controller.rb
@@ -48,20 +48,22 @@ def create
# GET /admin/pages/1/edit
def edit
- @page = Page.find_by_url params[:id]
+ @page = Page.find_by_url(params[:id])
@form_style = '' if @page.has_form
end
# PUT /admin/pages/1
def update
@page = Page.find_by_url(params[:id])
+ url = @page.url
respond_to do |format|
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
format.html { redirect_to admin_pages_url }
else
+ @page.url = url
format.html { render :action => "edit" }
end
end
View
7 app/models/page.rb
@@ -1,7 +1,8 @@
class Page < ActiveRecord::Base
- validates_presence_of :url, :message => 'Please fill in the URL'
- validates_uniqueness_of :url, :message => 'The URL is already in use. Please select another URL'
- validates_presence_of :title, :message => 'Please fill in the Title'
+ validates_presence_of :url, :message => 'Please fill in the URL.'
+ validates_uniqueness_of :url, :message => 'The URL is already in use. Please select another URL.'
+ validates_presence_of :title, :message => 'Please fill in the Title.'
+ validates_format_of :url, :with => /^[-a-z0-9_]{4,50}$/, :message => 'The URL must contain only alphanumerical characters, dashed and underscores.'
def to_param
self.url
View
24 db/schema.rb
@@ -108,18 +108,18 @@
t.datetime "created_at"
t.datetime "updated_at"
end
-
- create_table :pages, :force => true do |t|
- t.string :url
- t.string :page_title
- t.text :keywords
- t.text :description
- t.string :title
- t.text :content
- t.boolean :has_form
- t.text :form_message
- t.timestamps
- end
+ create_table "pages", :force => true do |t|
+ t.string "url"
+ t.string "page_title"
+ t.text "keywords"
+ t.text "description"
+ t.string "title"
+ t.text "content"
+ t.boolean "has_form"
+ t.text "form_message"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
end
View
36 test/fixtures/pages.yml
@@ -1,21 +1,19 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
-one:
- url: MyString
- page_title: MyString
- keywords: MyText
- description: MyText
- title: MyString
- content: MyText
+about:
+ url: about
+ page_title: About
+ keywords:
+ description:
+ title: About Us
+ content: Some content
has_form: false
- form_message: MyText
+ form_message:
-two:
- url: MyString
- page_title: MyString
- keywords: MyText
- description: MyText
- title: MyString
- content: MyText
- has_form: false
- form_message: MyText
+contact:
+ url: contact
+ page_title: Contact Us
+ keywords: contact, form
+ description: Contact form
+ title: Contact Us
+ content: If you have questions fill in the form
+ has_form: true
+ form_message: Thank you for message
View
2 test/functional/admin/categories_controller_test.rb
@@ -7,7 +7,7 @@ def test_should_show_category
assert_response :success
end
- def test_shouls_show_category_jobs
+ def test_should_show_category_jobs
login_as(:mark)
get :show, :id => categories(:programmer).name
assert_not_nil assigns(:jobs)
View
47 test/functional/admin/pages_controller_test.rb
@@ -1,8 +1,49 @@
require 'test_helper'
class Admin::PagesControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- def test_truth
- assert true
+
+ def test_should_get_pages_index
+ login_as(:admin)
+ get :index
+ assert_equal Page.count, assigns(:pages).size
end
+
+ def test_should_get_new
+ login_as :bob
+ get :new
+ assert_response :success
+ end
+
+ def test_should_create_page
+ login_as :bob
+ assert_difference('Page.count') do
+ post :create, :page => {:title => "Japan", :url => "country_japan"}
+ end
+ end
+
+ def test_should_redirect_after_creating
+ login_as :bob
+ post :create, :page => {:title => "About Us", :url => "about_us"}
+
+ assert_redirected_to admin_pages_path
+ end
+
+ def test_should_get_edit
+ login_as :admin
+ get :edit, :id => pages(:about).url
+ assert_response :success
+ end
+
+ def test_should_update_page
+ login_as :bob
+ put :update, :id => pages(:contact).url, :page => {:has_form => true, :content => '<b>Some text</b>'}
+ assert_response :redirect
+ end
+
+ def test_should_destroy_page
+ login_as :admin
+ assert_difference('Page.count', -1) do
+ xhr :delete, :destroy, :id => pages(:about).url
+ end
+ end
end
View
17 test/functional/pages_controller_test.rb
@@ -1,8 +1,19 @@
require 'test_helper'
class PagesControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- def test_truth
- assert true
+
+ def test_should_show_page
+ get :show, :id => pages(:about).url
+ assert_response :success
+ end
+
+ def test_should_show_page_form
+ get :show, :id => pages(:contact).url
+ assert_tag :tag => 'div', :attributes => { :id => 'contact-form'}
+ end
+
+ def test_should_not_show_page_form
+ get :show, :id => pages(:about).url
+ assert_no_tag :tag => 'div', :attributes => { :id => 'contact-form'}
end
end
View
57 test/integration/administrative_panel_test.rb
@@ -55,13 +55,11 @@ def test_manage_categories
get '/admin/categories'
assert_response :success
-
- count = Category.count
-
+
# add category
- post '/admin/categories'
- assert_response :redirect
- assert_not_equal(count, Category.count)
+ assert_difference("Category.count", 1) do
+ post '/admin/categories'
+ end
# update category name
put "/admin/categories/#{categories(:designer).id}", :name => 'New name', :url => categories(:designer).value
@@ -72,17 +70,52 @@ def test_manage_categories
put '/admin/categories/saveorder', :categoriesContainer => [categories(:designer).id,categories(:administrator).id,categories(:programmer).id]
assert_equal [1,2,3], [categories(:designer).reload.position, categories(:administrator).reload.position,categories(:programmer).reload.position]
- count = Category.count
# should not delete category with jobs
- delete "/admin/categories/#{categories(:designer).id}"
- assert_response :redirect
- assert_equal count, Category.count
+ assert_no_difference("Category.count") do
+ delete "/admin/categories/#{categories(:designer).id}"
+ end
# should delete category without jobs
- delete "/admin/categories/#{categories(:administrator).id}"
+ assert_difference("Category.count", -1) do
+ delete "/admin/categories/#{categories(:administrator).id}"
+ end
+
+ logout_user
+ end
+
+ def test_manage_pages
+ login_user
+
+ get '/admin/pages'
+ assert_response :success
+
+ get '/admin/pages/new'
+ assert_response :success
+
+ # add category
+ assert_difference("Page.count", 1) do
+ post '/admin/pages', :page => {:title => 'Title', :url => 'urls'}
+ end
+
+ assert_redirected_to admin_pages_url
+
+ get "/admin/pages/#{pages(:about).url}/edit"
+ assert_response :success
+
+ # update page
+ put "/admin/pages/#{pages(:about).url}", :page => {:title => 'New title', :content => 'Some content'}
assert_response :redirect
- assert_equal count - 1, Category.count
+
+ assert_redirected_to admin_pages_url
+
+ pages(:about).reload
+ assert_equal 'New title', pages(:about).title
+ # delete page
+ assert_difference("Page.count", -1) do
+ delete "/admin/pages/#{pages(:contact).url}"
+ end
+
logout_user
end
View
62 test/unit/page_test.rb
@@ -1,8 +1,64 @@
require 'test_helper'
class PageTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- def test_truth
- assert true
+
+ def test_valid
+ page = Page.new
+ page.title = 'Temporary page title'
+ page.url = 'page_url'
+
+ assert_valid page
+ end
+
+ def test_create
+ page = Page.new(:title => 'Some new title', :url => 'myurl')
+ page.save
+
+ assert_equal page, Page.find(page.id)
+ end
+
+ def test_have_title
+ page = Page.new(:url => "url")
+
+ assert !page.save
+ end
+
+ def test_have_title_error
+ page = Page.new(:url => "new_url")
+ page.save
+
+ assert_equal "Please fill in the Title.", page.errors[:title]
+ end
+
+ def test_have_url
+ page = Page.new(:title => "My title")
+
+ assert !page.save
+ end
+
+ def test_have_url_error
+ page = Page.new(:title => "Hello there")
+ page.save
+
+ assert_equal ["Please fill in the URL.",
+ "The URL must contain only alphanumerical characters, dashed and underscores."],
+ page.errors[:url]
+ end
+
+ def test_have_unique_url
+ page = Page.new(:title => 'Foo', :url => 'unique_url')
+ page.save
+
+ page1 = Page.new(:title => 'Foo foo', :url => 'unique_url')
+ assert !page1.save
+ end
+
+ def test_have_unique_url_error
+ page = Page.new(:title => 'Foo', :url => 'unique_url')
+ page.save
+
+ page1 = Page.new(:title => 'Foo foo', :url => 'unique_url')
+ page1.save
+ assert_equal "The URL is already in use. Please select another URL.", page1.errors[:url]
end
end

0 comments on commit 6b587c9

Please sign in to comment.
Something went wrong with that request. Please try again.