diff --git a/Gemfile b/Gemfile
index 19cd0949f..efc9a890a 100644
--- a/Gemfile
+++ b/Gemfile
@@ -8,7 +8,7 @@ group :test do
gem "simplecov", :require => false
end
-gem "rails", "3.0.0"
+gem "rails", "3.0.3"
gem "pg"
gem "memcache-client", :require => "memcache"
gem "imagesize", :require => "image_size"
diff --git a/Gemfile.lock b/Gemfile.lock
index 6a59e09c8..28e84ce1d 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,12 +1,12 @@
GIT
remote: http://github.com/EmmanuelOga/ffaker.git
- revision: cbe849d
+ revision: 3298ebf33319b840f693a1a0ae2613eeb53e7dc4
specs:
- ffaker (0.4.0)
+ ffaker (1.0.0)
GIT
remote: http://github.com/mislav/will_paginate.git
- revision: b1a5bee
+ revision: b1a5beeec9f56ecbe3594fcdca76d92b6767ce50
branch: rails3
specs:
will_paginate (3.0.pre3)
@@ -15,92 +15,93 @@ GEM
remote: http://gemcutter.org/
specs:
abstract (1.0.0)
- actionmailer (3.0.0)
- actionpack (= 3.0.0)
- mail (~> 2.2.5)
- actionpack (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
+ actionmailer (3.0.3)
+ actionpack (= 3.0.3)
+ mail (~> 2.2.9)
+ actionpack (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
builder (~> 2.1.2)
erubis (~> 2.6.6)
- i18n (~> 0.4.1)
+ i18n (~> 0.4)
rack (~> 1.2.1)
- rack-mount (~> 0.6.12)
- rack-test (~> 0.5.4)
+ rack-mount (~> 0.6.13)
+ rack-test (~> 0.5.6)
tzinfo (~> 0.3.23)
- activemodel (3.0.0)
- activesupport (= 3.0.0)
+ activemodel (3.0.3)
+ activesupport (= 3.0.3)
builder (~> 2.1.2)
- i18n (~> 0.4.1)
- activerecord (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
- arel (~> 1.0.0)
+ i18n (~> 0.4)
+ activerecord (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ arel (~> 2.0.2)
tzinfo (~> 0.3.23)
- activeresource (3.0.0)
- activemodel (= 3.0.0)
- activesupport (= 3.0.0)
- activesupport (3.0.0)
- arel (1.0.1)
- activesupport (~> 3.0.0)
+ activeresource (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ activesupport (3.0.3)
+ arel (2.0.6)
builder (2.1.2)
daemons (1.1.0)
- delayed_job (2.0.3)
+ delayed_job (2.1.2)
+ activesupport (~> 3.0)
daemons
erubis (2.6.6)
abstract (>= 1.0.0)
- factory_girl (1.3.2)
- haml (3.0.22)
- i18n (0.4.2)
+ factory_girl (1.3.3)
+ haml (3.0.25)
+ i18n (0.5.0)
imagesize (0.1.1)
- mail (2.2.9)
+ mail (2.2.14)
activesupport (>= 2.3.6)
- i18n (~> 0.4.1)
+ i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mechanize (1.0.0)
nokogiri (>= 1.2.1)
memcache-client (1.8.5)
- meta_search (0.9.7.2)
- actionpack (~> 3.0.0)
- activerecord (~> 3.0.0)
- activesupport (~> 3.0.0)
- arel (~> 1.0.1)
+ meta_search (0.9.11)
+ actionpack (~> 3.0.2)
+ activerecord (~> 3.0.2)
+ activesupport (~> 3.0.2)
+ arel (~> 2.0.2)
mime-types (1.16)
- mocha (0.9.9)
+ mocha (0.9.10)
rake
- nokogiri (1.4.3.1)
- pg (0.9.0)
+ nokogiri (1.4.4)
+ pg (0.10.0)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
- rack-test (0.5.6)
+ rack-test (0.5.7)
rack (>= 1.0)
- rails (3.0.0)
- actionmailer (= 3.0.0)
- actionpack (= 3.0.0)
- activerecord (= 3.0.0)
- activeresource (= 3.0.0)
- activesupport (= 3.0.0)
- bundler (~> 1.0.0)
- railties (= 3.0.0)
- railties (3.0.0)
- actionpack (= 3.0.0)
- activesupport (= 3.0.0)
- rake (>= 0.8.4)
- thor (~> 0.14.0)
+ rails (3.0.3)
+ actionmailer (= 3.0.3)
+ actionpack (= 3.0.3)
+ activerecord (= 3.0.3)
+ activeresource (= 3.0.3)
+ activesupport (= 3.0.3)
+ bundler (~> 1.0)
+ railties (= 3.0.3)
+ railties (3.0.3)
+ actionpack (= 3.0.3)
+ activesupport (= 3.0.3)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
rake (0.8.7)
shoulda (2.11.3)
- simple_form (1.2.2)
- simplecov (0.3.7)
+ simple_form (1.3.0)
+ rails (~> 3.0.0)
+ simplecov (0.3.9)
simplecov-html (>= 0.3.7)
simplecov-html (0.3.9)
super_exception_notifier (3.0.13)
actionmailer
rake
- thor (0.14.3)
- treetop (1.4.8)
+ thor (0.14.6)
+ treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
@@ -119,7 +120,7 @@ DEPENDENCIES
mocha
nokogiri
pg
- rails (= 3.0.0)
+ rails (= 3.0.3)
shoulda
simple_form
simplecov
diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb
index 25b87e4f0..d4348b615 100644
--- a/app/controllers/forum_topics_controller.rb
+++ b/app/controllers/forum_topics_controller.rb
@@ -1,42 +1,53 @@
class ForumTopicsController < ApplicationController
+ respond_to :html, :xml, :json
+ before_filter :member_only, :except => [:index, :show]
+ rescue_from User::PrivilegeError, :with => "static/access_denied"
+
def new
@forum_topic = ForumTopic.new
+ respond_with(@forum_topic)
end
def edit
@forum_topic = ForumTopic.find(params[:id])
+ check_privilege(@forum_topic)
+ respond_with(@forum_topic)
end
def index
@search = ForumTopic.search(params[:search])
@forum_topics = @search.paginate(:page => params[:page], :order => "updated_at DESC")
+ respond_with(@forum_topics)
end
def show
@forum_topic = ForumTopic.find(params[:id])
+ respond_with(@forum_topic)
end
def create
- @forum_topic = ForumTopic.new(params[:forum_topic])
- if @forum_topic.save
- redirect_to forum_topic_path(@forum_topic)
- else
- render :action => "new"
- end
+ @forum_topic = ForumTopic.create(params[:forum_topic])
+ respond_with(@forum_topic)
end
def update
@forum_topic = ForumTopic.find(params[:id])
- if @forum_topic.update_attributes(params[:forum_topic])
- redirect_to forum_topic_path(@forum_topic)
- else
- render :action => "edit"
- end
+ check_privilege(@forum_topic)
+ @forum_topic.update_attributes(params[:forum_topic])
+ respond_with(@forum_topic)
end
def destroy
@forum_topic = ForumTopic.find(params[:id])
+ check_privilege(@forum_topic)
@forum_topic.destroy
- redirect_to forum_topics_path
- end
+ respond_with(@forum_topic)
+ end
+
+ private
+ def check_privilege(forum_topic)
+ if !forum_topic.editable_by?(CurrentUser.user)
+ raise User::PrivilegeError
+ end
+ end
end
diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb
index 597dfb6fb..8878c5061 100644
--- a/app/models/forum_post.rb
+++ b/app/models/forum_post.rb
@@ -2,11 +2,22 @@ class ForumPost < ActiveRecord::Base
attr_accessible :body, :topic_id
belongs_to :creator, :class_name => "User"
belongs_to :topic, :class_name => "ForumTopic"
+ before_validation :initialize_creator, :on => :create
+ before_validation :initialize_updater
after_save :update_topic_updated_at
- validates_presence_of :body, :topic_id, :creator_id
- scope :search_body, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])}
+ validates_presence_of :body, :creator_id
+ scope :body_matches, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])}
+ search_methods :body_matches
def update_topic_updated_at
- topic.touch
+ topic.update_attributes(:updater_id => CurrentUser.id)
+ end
+
+ def initialize_creator
+ self.creator_id = CurrentUser.id
+ end
+
+ def initialize_updater
+ self.updater_id = CurrentUser.id
end
end
diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb
index 81a1844fa..910b81a60 100644
--- a/app/models/forum_topic.rb
+++ b/app/models/forum_topic.rb
@@ -1,8 +1,25 @@
class ForumTopic < ActiveRecord::Base
- attr_accessible :title
+ attr_accessible :title, :original_post_attributes
belongs_to :creator, :class_name => "User"
- has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc"
+ belongs_to :updater, :class_name => "User"
+ has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id"
+ has_one :original_post, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id"
+ before_validation :initialize_creator, :on => :create
+ before_validation :initialize_updater
validates_presence_of :title, :creator_id
- scope :search_title, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
- accepts_nested_attributes_for :posts
+ scope :title_matches, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
+ search_methods :title_matches
+ accepts_nested_attributes_for :original_post
+
+ def editable_by?(user)
+ creator_id == user.id || user.is_moderator?
+ end
+
+ def initialize_creator
+ self.creator_id = CurrentUser.id
+ end
+
+ def initialize_updater
+ self.updater_id = CurrentUser.id
+ end
end
diff --git a/app/views/forum_topics/_search.html.erb b/app/views/forum_topics/_search.html.erb
new file mode 100644
index 000000000..c268cf565
--- /dev/null
+++ b/app/views/forum_topics/_search.html.erb
@@ -0,0 +1,7 @@
+
+ <%= simple_form_for @search do |f| %>
+ <%= f.input :title_matches %>
+ <%= f.input :creator_name_equals %>
+ <%= f.button :submit %>
+ <% end %>
+
diff --git a/app/views/forum_topics/edit.html.erb b/app/views/forum_topics/edit.html.erb
new file mode 100644
index 000000000..150ff7cfa
--- /dev/null
+++ b/app/views/forum_topics/edit.html.erb
@@ -0,0 +1,12 @@
+Edit Topic
+
+<%= simple_form_for(@forum_topic) do |f| %>
+ <%= f.input :title %>
+
+ <%= f.simple_fields_for :original_post do |pf| %>
+ <%= text_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %>
+ <%= pf.input :body %>
+ <% end %>
+
+ <%= f.button :submit %>
+<% end %>
diff --git a/app/views/forum_topics/index.html.erb b/app/views/forum_topics/index.html.erb
new file mode 100644
index 000000000..efb4334ec
--- /dev/null
+++ b/app/views/forum_topics/index.html.erb
@@ -0,0 +1,24 @@
+Forum
+
+<%= render "search" %>
+
+
+
+
+ | Creator |
+ Title |
+ Updated by |
+ Updated at |
+
+
+
+ <% @forum_topics.each do |topic| %>
+
+ | <%= topic.creator.name %> |
+ <%= topic.title %> |
+ <%= topic.updater.name %> |
+ <%= compact_time topic.updated_at %> |
+
+ <% end %>
+
+
diff --git a/app/views/forum_topics/new.html.erb b/app/views/forum_topics/new.html.erb
new file mode 100644
index 000000000..a93745c68
--- /dev/null
+++ b/app/views/forum_topics/new.html.erb
@@ -0,0 +1,11 @@
+New Topic
+
+<%= simple_form_for(@forum_topic) do |f| %>
+ <%= f.input :title %>
+
+ <%= f.simple_fields_for :original_post do |pf| %>
+ <%= pf.input :body %>
+ <% end %>
+
+ <%= f.button :submit %>
+<% end %>
diff --git a/db/development_structure.sql b/db/development_structure.sql
index a08ab56c0..8bc0fc532 100644
--- a/db/development_structure.sql
+++ b/db/development_structure.sql
@@ -400,7 +400,6 @@ CREATE TABLE dmails (
owner_id integer NOT NULL,
from_id integer NOT NULL,
to_id integer NOT NULL,
- parent_id integer,
title character varying(255) NOT NULL,
body text NOT NULL,
message_index tsvector NOT NULL,
@@ -738,6 +737,7 @@ CREATE TABLE forum_posts (
id integer NOT NULL,
topic_id integer NOT NULL,
creator_id integer NOT NULL,
+ updater_id integer NOT NULL,
body text NOT NULL,
text_index tsvector NOT NULL,
created_at timestamp without time zone,
@@ -771,6 +771,7 @@ ALTER SEQUENCE forum_posts_id_seq OWNED BY forum_posts.id;
CREATE TABLE forum_topics (
id integer NOT NULL,
creator_id integer NOT NULL,
+ updater_id integer NOT NULL,
title character varying(255) NOT NULL,
response_count integer DEFAULT 0 NOT NULL,
is_sticky boolean DEFAULT false NOT NULL,
@@ -2386,13 +2387,6 @@ CREATE INDEX index_dmails_on_message_index ON dmails USING gin (message_index);
CREATE INDEX index_dmails_on_owner_id ON dmails USING btree (owner_id);
---
--- Name: index_dmails_on_parent_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
---
-
-CREATE INDEX index_dmails_on_parent_id ON dmails USING btree (parent_id);
-
-
--
-- Name: index_favorites_0_on_post_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
diff --git a/db/migrate/20100221003655_create_forum_topics.rb b/db/migrate/20100221003655_create_forum_topics.rb
index 9714dc422..4c727dfc8 100644
--- a/db/migrate/20100221003655_create_forum_topics.rb
+++ b/db/migrate/20100221003655_create_forum_topics.rb
@@ -2,6 +2,7 @@ class CreateForumTopics < ActiveRecord::Migration
def self.up
create_table :forum_topics do |t|
t.column :creator_id, :integer, :null => false
+ t.column :updater_id, :integer, :null => false
t.column :title, :string, :null => false
t.column :response_count, :integer, :null => false, :default => 0
t.column :is_sticky, :boolean, :null => false, :default => false
diff --git a/db/migrate/20100221005812_create_forum_posts.rb b/db/migrate/20100221005812_create_forum_posts.rb
index 12d388d73..b2bd2b0af 100644
--- a/db/migrate/20100221005812_create_forum_posts.rb
+++ b/db/migrate/20100221005812_create_forum_posts.rb
@@ -3,12 +3,13 @@ class CreateForumPosts < ActiveRecord::Migration
create_table :forum_posts do |t|
t.column :topic_id, :integer, :null => false
t.column :creator_id, :integer, :null => false
+ t.column :updater_id, :integer, :null => false
t.column :body, :text, :null => false
t.column :text_index, "tsvector", :null => false
t.timestamps
end
- add_index :forum_posts, :topic_id
+ add_index :forum_posts, :topic_id
add_index :forum_posts, :creator_id
execute "CREATE INDEX index_forum_posts_on_text_index ON forum_posts USING GIN (text_index)"
execute "CREATE TRIGGER trigger_forum_posts_on_update BEFORE INSERT OR UPDATE ON forum_posts FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('text_index', 'pg_catalog.english', 'body')"
diff --git a/test/factories/forum_post.rb b/test/factories/forum_post.rb
index 2b491e82a..8b2f32546 100644
--- a/test/factories/forum_post.rb
+++ b/test/factories/forum_post.rb
@@ -1,4 +1,3 @@
Factory.define(:forum_post) do |f|
- f.creator {|x| x.association(:user)}
f.body {Faker::Lorem.sentences}
end
diff --git a/test/factories/forum_topic.rb b/test/factories/forum_topic.rb
index ba053c9ff..0c15642b3 100644
--- a/test/factories/forum_topic.rb
+++ b/test/factories/forum_topic.rb
@@ -1,5 +1,4 @@
Factory.define(:forum_topic) do |f|
- f.creator {|x| x.association(:user)}
f.title {Faker::Lorem.words}
f.is_sticky false
f.is_locked false
diff --git a/test/functional/forum_topics_controller_test.rb b/test/functional/forum_topics_controller_test.rb
index 289550cd9..84af303a0 100644
--- a/test/functional/forum_topics_controller_test.rb
+++ b/test/functional/forum_topics_controller_test.rb
@@ -1,8 +1,80 @@
require 'test_helper'
class ForumTopicsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+ context "The forum topics controller" do
+ setup do
+ @user = Factory.create(:user)
+ CurrentUser.user = @user
+ CurrentUser.ip_addr = "127.0.0.1"
+ @other_user = Factory.create(:user)
+ @mod = Factory.create(:moderator_user)
+ @forum_topic = Factory.create(:forum_topic, :title => "my forum topic", :creator => @user)
+ end
+
+ teardown do
+ CurrentUser.user = nil
+ CurrentUser.ip_addr = nil
+ end
+
+ context "index action" do
+ should "list all forum topics" do
+ get :index
+ assert_response :success
+ end
+
+ context "with search conditions" do
+ should "list all matching forum topics" do
+ get :index, {:search => {:title_matches => "forum"}}
+ assert_response :success
+ assert_equal(1, assigns(:forum_topics).size)
+ end
+
+ should "list nothing for when the search matches nothing" do
+ get :index, {:search => {:title_matches => "bababa"}}
+ assert_response :success
+ assert_equal(0, assigns(:forum_topics).size)
+ end
+ end
+ end
+
+ context "edit action" do
+ should "render if the editor is the creator of the topic" do
+ get :edit, {:id => @forum_topic.id}, {:user_id => @user.id}
+ assert_response :success
+ end
+
+ should "render if the editor is a moderator" do
+ get :edit, {:id => @forum_topic.id}, {:user_id => @mod.id}
+ assert_response :success
+ end
+
+ should "fail if the editor is not the creator of the topic and is not a moderator" do
+ assert_raises(User::PrivilegeError) do
+ get :edit, {:id => @forum_topic.id}, {:user_id => @other_user.id}
+ end
+ end
+ end
+
+ context "new action" do
+ should "render" do
+ get :new, {}, {:user_id => @user.id}
+ assert_response :success
+ end
+ end
+
+ context "create action" do
+ should "create a new forum topic and post" do
+ assert_difference(["ForumPost.count", "ForumTopic.count"], 1) do
+ post :create, {:forum_topic => {:title => "bababa", :original_post_attributes => {:body => "xaxaxa"}}}, {:user_id => @user.id}
+ end
+
+ forum_topic = ForumTopic.last
+ assert_redirected_to(forum_topic_path(forum_topic))
+ end
+ end
+
+ context "destroy action" do
+
+ end
end
end
diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb
index a46656572..41e9cb4d4 100644
--- a/test/unit/dmail_test.rb
+++ b/test/unit/dmail_test.rb
@@ -18,7 +18,7 @@ class DmailTest < ActiveSupport::TestCase
context "search" do
should "return results based on title contents" do
- dmail = Factory.create(:dmail, :title => "xxx")
+ dmail = Factory.create(:dmail, :title => "xxx", :owner => @user)
matches = Dmail.search_message("xxx")
assert(matches.any?)
matches = Dmail.search_message("aaa")
@@ -26,7 +26,7 @@ class DmailTest < ActiveSupport::TestCase
end
should "return results based on body contents" do
- dmail = Factory.create(:dmail, :body => "xxx")
+ dmail = Factory.create(:dmail, :body => "xxx", :owner => @user)
matches = Dmail.search_message("xxx")
assert(matches.any?)
matches = Dmail.search_message("aaa")
@@ -35,14 +35,14 @@ class DmailTest < ActiveSupport::TestCase
end
should "should parse user names" do
- dmail = Factory.build(:dmail)
+ dmail = Factory.build(:dmail, :owner => @user)
dmail.to_id = nil
dmail.to_name = @user.name
assert(dmail.to_id == @user.id)
end
should "construct a response" do
- dmail = Factory.create(:dmail)
+ dmail = Factory.create(:dmail, :owner => @user)
response = dmail.build_response
assert_equal("Re: #{dmail.title}", response.title)
assert_equal(dmail.from_id, response.to_id)
@@ -58,19 +58,19 @@ class DmailTest < ActiveSupport::TestCase
should "send an email if the user wants it" do
user = Factory.create(:user, :receive_email_notifications => true)
assert_difference("ActionMailer::Base.deliveries.size", 1) do
- Factory.create(:dmail, :to => user)
+ Factory.create(:dmail, :to => user, :owner => @user)
end
end
should "be marked as read after the user reads it" do
- dmail = Factory.create(:dmail)
+ dmail = Factory.create(:dmail, :owner => @user)
assert(!dmail.is_read?)
dmail.mark_as_read!
assert(dmail.is_read?)
end
should "notify the recipient he has mail" do
- dmail = Factory.create(:dmail)
+ dmail = Factory.create(:dmail, :owner => @user)
assert(dmail.to(true).has_mail?)
dmail.mark_as_read!
assert(!dmail.to(true).has_mail?)
diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb
index 3541e55c9..f27f92ea1 100644
--- a/test/unit/forum_post_test.rb
+++ b/test/unit/forum_post_test.rb
@@ -2,12 +2,47 @@ require_relative '../test_helper'
class ForumPostTest < ActiveSupport::TestCase
context "A forum post" do
+ setup do
+ @user = Factory.create(:user)
+ CurrentUser.user = @user
+ CurrentUser.ip_addr = "127.0.0.1"
+ @topic = Factory.create(:forum_topic)
+ end
+
+ teardown do
+ CurrentUser.user = nil
+ CurrentUser.ip_addr = nil
+ end
+
should "update its parent when saved" do
- topic = Factory.create(:forum_topic)
- sleep 2
- post = Factory.create(:forum_post, :topic_id => topic.id)
- topic.reload
- assert(topic.updated_at > 1.second.ago)
+ sleep 1
+ post = Factory.create(:forum_post, :topic_id => @topic.id)
+ @topic.reload
+ assert(@topic.updated_at > 1.second.ago)
+ end
+
+ should "be searchable by body content" do
+ post = Factory.create(:forum_post, :topic_id => @topic.id, :body => "xxx")
+ assert_equal(1, ForumPost.body_matches("xxx").count)
+ assert_equal(0, ForumPost.body_matches("aaa").count)
+ end
+
+ should "initialize its creator" do
+ post = Factory.create(:forum_post, :topic_id => @topic.id)
+ assert_equal(@user.id, post.creator_id)
+ end
+
+ context "updated by a second user" do
+ setup do
+ @post = Factory.create(:forum_post, :topic_id => @topic.id)
+ @second_user = Factory.create(:user)
+ CurrentUser.user = @second_user
+ end
+
+ should "record its updater" do
+ @post.update_attributes(:body => "abc")
+ assert_equal(@second_user.id, @post.updater_id)
+ end
end
end
end
diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb
index eda413934..54a7c12b8 100644
--- a/test/unit/forum_topic_test.rb
+++ b/test/unit/forum_topic_test.rb
@@ -1,4 +1,46 @@
require_relative '../test_helper'
class ForumTopicTest < ActiveSupport::TestCase
+ context "A forum topic" do
+ setup do
+ @user = Factory.create(:user)
+ CurrentUser.user = @user
+ CurrentUser.ip_addr = "127.0.0.1"
+ @topic = Factory.create(:forum_topic, :title => "xxx")
+ end
+
+ teardown do
+ CurrentUser.user = nil
+ CurrentUser.ip_addr = nil
+ end
+
+ context "constructed with nested attributes for its original post" do
+ should "create a matching forum post" do
+ assert_difference(["ForumTopic.count", "ForumPost.count"], 1) do
+ @topic = Factory.create(:forum_topic, :title => "abc", :original_post_attributes => {:body => "abc"})
+ end
+ end
+ end
+
+ should "be searchable by title" do
+ assert_equal(1, ForumTopic.title_matches("xxx").count)
+ assert_equal(0, ForumTopic.title_matches("aaa").count)
+ end
+
+ should "initialize its creator" do
+ assert_equal(@user.id, @topic.creator_id)
+ end
+
+ context "updated by a second user" do
+ setup do
+ @second_user = Factory.create(:user)
+ CurrentUser.user = @second_user
+ end
+
+ should "record its updater" do
+ @topic.update_attributes(:title => "abc")
+ assert_equal(@second_user.id, @topic.updater_id)
+ end
+ end
+ end
end