<commit_before>require 'spec_helper'

describe Post, 'validations' do
  it { should validate_presence_of(:content) }
  it { should validate_presence_of(:messageboard_id) }
end

describe Post, 'associations' do
  it { should have_many(:post_notifications) }
end

describe Post, '#create' do
  before(:each) do
    Time.zone = 'UTC'
    Chronic.time_class = Time.zone
  end

  after(:each) do
    Timecop.return
  end

  it 'updates the parent topic with the latest post author' do
    joel  = create(:user)
    topic = create(:topic)
    post = create(:post, user: joel, topic: topic)

    topic.reload.last_user.should eq joel
  end

  it "increments the topic's and user's post counts" do
    joel  = create(:user)
    topic = create(:topic)

    3.times do
      topic.posts.create!(user: joel, last_user: joel, content: 'content',
        messageboard: topic.messageboard)
    end

    topic.reload.posts_count.should eq 3
    joel.reload.posts_count.should eq 3
  end

  it 'updates the topic updated_at field to that of the new post' do
    joel  = create(:user)
    topic = create(:topic)
    messageboard = topic.messageboard

    Timecop.travel(Chronic.parse('Jan 1st 2012 at noon')) do
      create(:post, topic: topic, messageboard: messageboard, user: joel, content: 'posting here')
    end

    Timecop.travel(Chronic.parse('Jan 1st 2012 at 3pm')) do
      create(:post, topic: topic, messageboard: messageboard, user: joel, content: 'posting more')
    end

    topic.updated_at.to_s.should eq Chronic.parse('Jan 1st 2012 at 3pm').to_s
  end

  it 'sets the post user email on creation' do
    shaun = create(:user)
    topic = create(:topic, last_user: shaun)
    post = create(:post, user: shaun, topic: topic)

    post.user_email.should eq post.user.email
  end
end

describe Post, '.filtered_content' do
  before(:each) do
    @post  = build(:post)
  end

  it 'converts textile to html' do
    @post.content = 'this is *bold*'
    @post.filter = 'textile'
    @post.filtered_content.should eq '<p>this is <strong>bold</strong></p>'
  end

  it 'converts bbcode to html' do
    @post.content = 'this is [b]bold[/b]'
    @post.filter = 'bbcode'
    @post.filtered_content.should eq 'this is <strong>bold</strong>'
  end

  it 'handles quotes' do
    @post.content = '[quote]hi[/quote] [quote="john"]hey[/quote]'
    @post.filter = 'bbcode'
    expected_output = '</p><fieldset><blockquote><p>hi</p></blockquote></fieldset><fieldset><legend>"john"</legend><blockquote><p>hey</p></blockquote></fieldset><p>'
    @post.filtered_content.should eq expected_output
  end<commit_msg>[JRO] Add spec for [link] [/link] bbcode<commit_after>require 'spec_helper'

describe Post, 'validations' do
  it { should validate_presence_of(:content) }
  it { should validate_presence_of(:messageboard_id) }
end

describe Post, 'associations' do
  it { should have_many(:post_notifications) }
end

describe Post, '#create' do
  before(:each) do
    Time.zone = 'UTC'
    Chronic.time_class = Time.zone
  end

  after(:each) do
    Timecop.return
  end

  it 'updates the parent topic with the latest post author' do
    joel  = create(:user)
    topic = create(:topic)
    post = create(:post, user: joel, topic: topic)

    topic.reload.last_user.should eq joel
  end

  it "increments the topic's and user's post counts" do
    joel  = create(:user)
    topic = create(:topic)

    3.times do
      topic.posts.create!(user: joel, last_user: joel, content: 'content',
        messageboard: topic.messageboard)
    end

    topic.reload.posts_count.should eq 3
    joel.reload.posts_count.should eq 3
  end

  it 'updates the topic updated_at field to that of the new post' do
    joel  = create(:user)
    topic = create(:topic)
    messageboard = topic.messageboard

    Timecop.travel(Chronic.parse('Jan 1st 2012 at noon')) do
      create(:post, topic: topic, messageboard: messageboard, user: joel, content: 'posting here')
    end

    Timecop.travel(Chronic.parse('Jan 1st 2012 at 3pm')) do
      create(:post, topic: topic, messageboard: messageboard, user: joel, content: 'posting more')
    end

    topic.updated_at.to_s.should eq Chronic.parse('Jan 1st 2012 at 3pm').to_s
  end

  it 'sets the post user email on creation' do
    shaun = create(:user)
    topic = create(:topic, last_user: shaun)
    post = create(:post, user: shaun, topic: topic)

    post.user_email.should eq post.user.email
  end
end

describe Post, '.filtered_content' do
  before(:each) do
    @post  = build(:post)
  end

  it 'renders implied legacy links' do
    @post.content = 'go to [link]http://google.com[/link]'
    @post.filter = 'bbcode'

    @post.filtered_content.should eq 'go to <a href="http://google.com">http://google.com</a>'
  end

  it 'renders legacy links' do
    @post.content = 'let me [link=http://google.com]google[/link] that'
    @post.filter = 'bbcode'

    @post.filtered_content.should eq 'let me <a href="http://google.com">google</a> that'
  end

  it 'converts textile to html' do
    @post.content = 'this is *bold*'
    @post.filter = 'textile'
    @post.filtered_content.should eq '<p>this is <strong>bold</strong></p>'
  end

  it 'converts bbcode to html' do
    @post.content = 'this is [b]bold[/b]'
    @post.filter = 'bbcode'
    @post.filtered_content.should eq 'this is <strong>bold</strong>'
  end

  it 'handles quotes' do
    @post.content = '[quote]hi[/quote] [quote="john"]hey[/quote]'
    @post.filter = 'bbcode'
    expected_output = '</p><fieldset><blockquote><p>hi</p></blockquote></fieldset><fieldset><legend>"john"</legend><blockquote><p>hey</p></blockquote></fieldset><p>'
    @post.filtered_content.should eq expected_output
  end
{'fraction_non_alphanumeric': 0.1308411214953271, 'fraction_numerical': 0.007053429730206313, 'mean_word_length': 3.581583198707593, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067321', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1863, 'n_words': 522}