2019-05-12 23:04:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
require "rails_helper"
|
2019-05-12 23:04:27 -04:00
|
|
|
|
|
|
|
RSpec.describe TopicsController do
|
|
|
|
let(:p1) { Fabricate(:post, like_count: 1) }
|
|
|
|
let(:topic) { p1.topic }
|
|
|
|
let(:p2) { Fabricate(:post, like_count: 2, topic: topic, user: Fabricate(:user)) }
|
|
|
|
|
|
|
|
def schema_json(answerCount)
|
|
|
|
if answerCount > 0
|
2022-12-23 15:36:08 -05:00
|
|
|
answer_json =
|
|
|
|
',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"dateCreated":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}"}}' %
|
|
|
|
{
|
|
|
|
answer_text: p2.excerpt,
|
|
|
|
answer_likes: p2.like_count,
|
|
|
|
answered_at: p2.created_at.as_json,
|
|
|
|
answer_url: p2.full_url,
|
|
|
|
username2: p2.user&.username,
|
|
|
|
}
|
2019-05-12 23:04:27 -04:00
|
|
|
else
|
|
|
|
answer_json = ""
|
|
|
|
end
|
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
# rubocop:todo Layout/LineLength
|
|
|
|
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"QAPage","name":"%{title}","mainEntity":{"@type":"Question","name":"%{title}","text":"%{question_text}","upvoteCount":%{question_likes},"answerCount":%{answerCount},"dateCreated":"%{created_at}","author":{"@type":"Person","name":"%{username1}"}%{answer_json}}}</script>' %
|
|
|
|
# rubocop:enable Layout/LineLength
|
|
|
|
{
|
|
|
|
title: topic.title,
|
|
|
|
question_text: p1.excerpt,
|
|
|
|
question_likes: p1.like_count,
|
|
|
|
answerCount: answerCount,
|
|
|
|
created_at: topic.created_at.as_json,
|
|
|
|
username1: topic.user&.name,
|
|
|
|
answer_json: answer_json,
|
|
|
|
}
|
2019-05-12 23:04:27 -04:00
|
|
|
end
|
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
context "with solved enabled on every topic" do
|
|
|
|
before { SiteSetting.allow_solved_on_all_topics = true }
|
2021-09-17 10:12:47 -04:00
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
it "should include correct schema information in header" do
|
2021-09-17 10:12:47 -04:00
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
|
|
|
|
|
|
|
expect(response.body).to include(schema_json(0))
|
2019-05-12 23:04:27 -04:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
p2.custom_fields["is_accepted_answer"] = true
|
|
|
|
p2.save_custom_fields
|
|
|
|
topic.custom_fields["accepted_answer_post_id"] = p2.id
|
|
|
|
topic.save_custom_fields
|
2019-05-12 23:04:27 -04:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
2019-05-12 23:04:27 -04:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
expect(response.body).to include(schema_json(1))
|
|
|
|
end
|
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
it "should include quoted content in schema information" do
|
2021-09-17 10:12:47 -04:00
|
|
|
post = topic.first_post
|
|
|
|
post.raw = "[quote]This is a quoted text.[/quote]"
|
|
|
|
post.save!
|
|
|
|
post.rebake!
|
2019-05-12 23:04:27 -04:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
2019-05-12 23:04:27 -04:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
expect(response.body).to include('"text":"This is a quoted text."')
|
|
|
|
end
|
2023-06-01 11:50:50 -04:00
|
|
|
|
|
|
|
it "should include user name in output with the corresponding site setting" do
|
|
|
|
SiteSetting.display_name_on_posts = true
|
|
|
|
p2.custom_fields["is_accepted_answer"] = true
|
|
|
|
p2.save_custom_fields
|
|
|
|
topic.custom_fields["accepted_answer_post_id"] = p2.id
|
|
|
|
topic.save_custom_fields
|
|
|
|
|
|
|
|
get "/t/#{topic.slug}/#{topic.id}.json"
|
|
|
|
|
|
|
|
expect(response.parsed_body["accepted_answer"]["name"]).to eq(p2.user.name)
|
|
|
|
expect(response.parsed_body["accepted_answer"]["username"]).to eq(p2.user.username)
|
2023-06-05 09:54:25 -04:00
|
|
|
|
|
|
|
# enable_names is default ON, this ensures disabling it also disables names here
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
get "/t/#{topic.slug}/#{topic.id}.json"
|
|
|
|
expect(response.parsed_body["accepted_answer"]["name"]).to eq(nil)
|
|
|
|
expect(response.parsed_body["accepted_answer"]["username"]).to eq(p2.user.username)
|
2023-06-01 11:50:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include user name when site setting is disabled" do
|
|
|
|
SiteSetting.display_name_on_posts = false
|
|
|
|
p2.custom_fields["is_accepted_answer"] = true
|
|
|
|
p2.save_custom_fields
|
|
|
|
topic.custom_fields["accepted_answer_post_id"] = p2.id
|
|
|
|
topic.save_custom_fields
|
|
|
|
|
|
|
|
get "/t/#{topic.slug}/#{topic.id}.json"
|
|
|
|
|
|
|
|
expect(response.parsed_body["accepted_answer"]["name"]).to eq(nil)
|
|
|
|
expect(response.parsed_body["accepted_answer"]["username"]).to eq(p2.user.username)
|
|
|
|
end
|
2019-05-12 23:04:27 -04:00
|
|
|
end
|
2020-01-04 08:55:42 -05:00
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
context "with solved enabled for topics with specific tags" do
|
2021-09-17 10:12:47 -04:00
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
before { SiteSetting.enable_solved_tags = tag.name }
|
2020-01-04 08:55:42 -05:00
|
|
|
|
2022-12-23 15:36:08 -05:00
|
|
|
it "includes the correct schema information" do
|
2021-09-17 10:12:47 -04:00
|
|
|
DiscourseTagging.add_or_create_tags_by_name(topic, [tag.name])
|
|
|
|
p2.custom_fields["is_accepted_answer"] = true
|
|
|
|
p2.save_custom_fields
|
|
|
|
topic.custom_fields["accepted_answer_post_id"] = p2.id
|
|
|
|
topic.save_custom_fields
|
2020-01-04 08:55:42 -05:00
|
|
|
|
2021-09-17 10:12:47 -04:00
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
|
|
|
|
|
|
|
expect(response.body).to include(schema_json(1))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include solved schema information when the topic has a different tag" do
|
|
|
|
another_tag = Fabricate(:tag)
|
|
|
|
|
|
|
|
DiscourseTagging.add_or_create_tags_by_name(topic, [another_tag.name])
|
|
|
|
p2.custom_fields["is_accepted_answer"] = true
|
|
|
|
p2.save_custom_fields
|
|
|
|
topic.custom_fields["accepted_answer_post_id"] = p2.id
|
|
|
|
topic.save_custom_fields
|
|
|
|
|
|
|
|
get "/t/#{topic.slug}/#{topic.id}"
|
|
|
|
|
|
|
|
expect(response.body).not_to include(schema_json(1))
|
|
|
|
end
|
2020-01-04 08:55:42 -05:00
|
|
|
end
|
2019-05-12 23:04:27 -04:00
|
|
|
end
|