summaryrefslogtreecommitdiff
path: root/test/models/forum_post_test.rb
blob: b02e95c7e3f193340ba681159c4cc33b46449c1f (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
# frozen_string_literal: true

# == Schema Information
#
# Table name: forum_posts
#
#  id                :integer          not null, primary key
#  anonymous         :boolean          default(FALSE)
#  body              :string           not null
#  deletion_reason   :string           default(""), not null
#  destroyed_content :boolean          default(FALSE), not null
#  edit_reason       :string
#  edited_at         :datetime
#  hidden_from_users :boolean          default(FALSE), not null
#  ip                :inet
#  name_at_post_time :string
#  referrer          :string           default("")
#  topic_position    :integer          not null
#  tripcode          :text
#  user_agent        :string           default("")
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  deleted_by_id     :integer
#  topic_id          :integer          not null
#  user_id           :integer
#
# Indexes
#
#  index_forum_posts_on_deleted_by_id                (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)
#  index_forum_posts_on_topic_id_and_created_at      (topic_id,created_at)
#  index_forum_posts_on_topic_id_and_topic_position  (topic_id,topic_position)
#  index_forum_posts_on_user_id                      (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (deleted_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (topic_id => topics.id) ON DELETE => cascade ON UPDATE => cascade
#  fk_rails_...  (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class ForumPostTest < ActiveSupport::TestCase
  setup do
    @post = build(:forum_post)
  end

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

  test 'should update its parents on creation' do
    forum_count = @post.topic.forum.post_count
    topic_count = @post.topic.post_count

    @post.save!
    @post.reload
    assert_equal forum_count + 1, @post.topic.forum.post_count
    assert_equal topic_count + 1, @post.topic.post_count
    assert_equal @post, @post.topic.forum.last_post
    assert_equal @post.topic, @post.topic.forum.last_topic
  end

  test 'should update its parents when deleted' do
    @post.save

    ForumPostHider.new(@post, reason: 'test system deletion').save

    assert_not_equal @post.topic.forum.last_post, @post
    assert_not_equal @post.topic.last_post, @post
  end

  test 'should close open reports when hidden' do
    @report = create(:report_of_post)
    @post = @report.reportable
    ForumPostHider.new(@post, reason: 'I just want to close the report.').save

    @report.reload
    assert_equal 'closed', @report.state
  end

  test 'should hide threads when hidden post is the OP' do
    @post.save!

    ForumPostHider.new(@post.topic.posts.first, reason: 'test OP deletion').save

    @post.topic.reload
    assert @post.topic.hidden_from_users
    assert_equal 'test OP deletion', @post.topic.deletion_reason
  end
end