summaryrefslogtreecommitdiff
path: root/test/models/topic_test.rb
blob: f97880a880525bfc0a96a8b020cfe2da2ccc5bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

# == Schema Information
#
# Table name: topics
#
#  id                 :integer          not null, primary key
#  anonymous          :boolean          default(FALSE)
#  deletion_reason    :string
#  hidden_from_users  :boolean          default(FALSE), not null
#  last_replied_to_at :datetime
#  lock_reason        :string
#  locked_at          :datetime
#  post_count         :integer          default(0), not null
#  slug               :string           not null
#  sticky             :boolean          default(FALSE), not null
#  title              :string           not null
#  view_count         :integer          default(0), not null
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  deleted_by_id      :integer
#  forum_id           :integer          not null
#  last_post_id       :integer
#  locked_by_id       :integer
#  user_id            :integer
#
# Indexes
#
#  index_topics_on_deleted_by_id       (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)
#  index_topics_on_forum_id            (forum_id)
#  index_topics_on_hidden_from_users   (hidden_from_users)
#  index_topics_on_last_post_id        (last_post_id)
#  index_topics_on_last_replied_to_at  (last_replied_to_at)
#  index_topics_on_locked_by_id        (locked_by_id) WHERE (locked_by_id IS NOT NULL)
#  index_topics_on_slug                (slug)
#  index_topics_on_sticky              (sticky)
#  index_topics_on_user_id             (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (deleted_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (forum_id => forums.id) ON DELETE => cascade ON UPDATE => cascade
#  fk_rails_...  (last_post_id => forum_posts.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (locked_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class TopicTest < ActiveSupport::TestCase
  setup do
    @topic = build(:topic)
  end

  test 'should be valid' do
    assert @topic.valid?
  end

  test '#page_for_post should return the correct page for posts' do
    @topic = create(:topic)
    @posts = []
    26.times { @posts << create(:post, topic: @topic) }
    @topic.reload
    assert_equal 1, @topic.page_for_post(@posts[0].id)
    assert_equal 1, @topic.page_for_post(@posts[23].id)
    assert_equal 2, @topic.page_for_post(@posts[24].id)
    assert_equal 2, @topic.page_for_post(@posts[25].id)
  end

  test 'should set its slug' do
    @topic.save
    assert @topic.slug.present?
  end

  test 'should choose a unique slug correctly for identical names in differing forums' do
    @topic.title = 'This is a title'
    @topic.save
    @forum2 = create(:forum)
    @topic2 = create(:topic, title: 'This is a title', forum: @forum2)

    assert_equal @topic2.slug, @topic.slug
  end

  test 'should choose a unique slug correctly for identical names in the same forum' do
    @topic.title = 'This is a title'
    @topic.save
    assert_not_equal Topic.generate_unique_slug('This is a title', @topic.forum.id), @topic.slug
  end

  test 'should update its forum when created' do
    assert_difference('@topic.forum.topic_count') { @topic.save }
  end

  test "should update its author's topic count when created" do
    assert_difference('@topic.user.topic_count') { @topic.save }
  end

  test 'should update its forum when deleted' do
    @topic = create(:topic_with_posts)
    assert_difference('@topic.forum.topic_count', -1) do
      TopicHider.new(@topic, reason: 'Because the test environment is a harsh place').save
    end
  end

  test "should decrement its forum's post count when deleted" do
    @topic = create(:topic_with_posts)
    assert_difference('@topic.forum.post_count', [email protected]_count) do
      TopicHider.new(@topic, reason: 'Because the test environment is a harsh place').save
    end
  end

  test '#move_to_forum! migrates completely to the new forum' do
    @target_forum = create(:forum)
    @old_forum = create(:forum)
    @topic = create(:topic_with_posts, forum: @old_forum)
    @topic.save

    assert_equal 1, @old_forum.topic_count
    # assert_equal @topic.post_count, @old_forum.post_count
    # assert_equal @topic.last_post, @old_forum.last_post

    @topic.move_to_forum!(@target_forum)

    @target_forum.reload
    @old_forum.reload

    assert_equal 0, @old_forum.topic_count
    assert_equal 0, @old_forum.post_count
    assert_nil @old_forum.last_post

    assert_equal 1, @target_forum.topic_count
    assert_equal @topic.post_count, @target_forum.post_count
    assert_equal @topic.last_post, @target_forum.last_post
  end
end