A platform for community discussion. Free, open, simple.
FIX: Skip QAPage schema when topic has no visible answers (#38964)
Régis Hanolaf870d50f849
diff --git a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rbindex 39396ab779b..a4f378ef6e6 100644--- a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb+++ b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb@@ -14,6 +14,7 @@ class DiscourseSolved::BuildSchemaMarkuppolicy :schema_markup_enabledmodel :accepted_answer, optional: truemodel :suggested_answers, optional: true+ policy :has_answersmodel :htmlprivate@@ -30,8 +31,13 @@ class DiscourseSolved::BuildSchemaMarkupDiscourseSolved::SchemaUtils.schema_markup_enabled?(topic)end+ def has_answers(accepted_answer:, suggested_answers:)+ accepted_answer.present? || suggested_answers.present?+ end+def fetch_accepted_answer(topic:)- topic.solved&.answer_post+ post = topic.solved&.answer_post+ post if post.present? && Guardian.new.can_see_post?(post)enddef fetch_suggested_answers(params:, topic:, accepted_answer:)diff --git a/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rbindex d2e76f5069f..2fe2bea6dfd 100644--- a/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb+++ b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb@@ -19,31 +19,54 @@ module DiscourseSolvedwhen "never"falsewhen "answered only"- topic.solved&.answer_post_id.present?+ accepted_answer_visible?(topic)elsetrueendend+ def self.qa_page_schema?(topic)+ if topic.instance_variable_defined?(:@qa_page_schema)+ return topic.instance_variable_get(:@qa_page_schema)+ end+ topic.instance_variable_set(+ :@qa_page_schema,+ schema_markup_enabled?(topic) && has_eligible_answers?(topic),+ )+ end+def self.container_schema(topic)- return nil if !schema_markup_enabled?(topic)+ return nil unless qa_page_schema?(topic){ itemscope: true, itemtype: "https://schema.org/QAPage" }enddef self.main_entity_schema(topic)- return nil if !schema_markup_enabled?(topic)+ return nil unless qa_page_schema?(topic){ itemprop: "mainEntity", itemscope: true, itemtype: "https://schema.org/Question" }enddef self.post_schema(post, topic)- return nil if !schema_markup_enabled?(topic)+ return nil unless qa_page_schema?(topic)return {} if post.is_first_post?return { itemscope: true } if post.post_type == Post.types[:small_action]- if topic.solved&.answer_post_id == post.id+ if accepted_answer_visible?(topic) && topic.solved.answer_post_id == post.id{ itemprop: "acceptedAnswer", itemscope: true, itemtype: "https://schema.org/Answer" }else{ itemprop: "suggestedAnswer", itemscope: true, itemtype: "https://schema.org/Answer" }endend++ private_class_method def self.accepted_answer_visible?(topic)+ post = topic.solved&.answer_post+ post.present? && Guardian.new.can_see_post?(post)+ end++ private_class_method def self.has_eligible_answers?(topic)+ topic+ .posts+ .where.not(post_number: 1)+ .where(post_type: Post.types[:regular], hidden: false)+ .exists?+ endendenddiff --git a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb b/plugins/discourse-solved/spec/requests/topics_controller_spec.rbindex 20a286fd663..bc502c4d9e7 100644--- a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb+++ b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb@@ -3,54 +3,53 @@RSpec.describe TopicsController dolet(: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- answer_json =- ',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"datePublished":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}","url":"%{user2_url}"}}' %- {- 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,- user2_url: p2.user&.full_url,- }- else- answer_json = ""- end-- '<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},"datePublished":"%{created_at}","author":{"@type":"Person","name":"%{username1}","url":"%{user1_url}"}%{answer_json}}}</script>' %+ let(:p2) { Fabricate(:post, like_count: 2, topic:, user: Fabricate(:user)) }++ def expected_schema_json+ answer_json =+ ',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"datePublished":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}","url":"%{user2_url}"}}' %+ {+ 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,+ user2_url: p2.user&.full_url,+ }++ '<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":1,"datePublished":"%{created_at}","author":{"@type":"Person","name":"%{username1}","url":"%{user1_url}"}%{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&.username,user1_url: topic.user&.full_url,- answer_json: answer_json,+ answer_json:,}endcontext "with solved enabled on every topic" dobefore { SiteSetting.allow_solved_on_all_topics = true }- it "should include correct schema information in header" do+ it "should not include schema information for single-post topics without answers" doget "/t/#{topic.slug}/#{topic.id}"- expect(response.body).to include(schema_json(0))+ expect(response.body).not_to include("QAPage")+ end- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ it "should include correct schema information when topic has an accepted answer" do+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}"- expect(response.body).to include(schema_json(1))+ expect(response.body).to include(expected_schema_json)endit "should include quoted content in schema information" do+ Fabricate(:solved_topic, topic:, answer_post: p2)+post = topic.first_postpost.raw = "[quote]This is a quoted text.[/quote]"post.save!@@ -65,7 +64,7 @@ RSpec.describe TopicsController doSiteSetting.display_name_on_posts = trueSiteSetting.show_who_marked_solved = trueaccepter = Fabricate(:user)- Fabricate(:solved_topic, topic: topic, answer_post: p2, accepter:)+ Fabricate(:solved_topic, topic:, answer_post: p2, accepter:)get "/t/#{topic.slug}/#{topic.id}.json"@@ -88,7 +87,7 @@ RSpec.describe TopicsController doit "should not include user name when site setting is disabled" doSiteSetting.display_name_on_posts = false- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}.json"@@ -106,7 +105,9 @@ RSpec.describe TopicsController doNokogiri::HTML5.fragment(response.body)end- it "uses Question schema instead of DiscussionForumPosting" do+ it "uses Question schema instead of DiscussionForumPosting when topic has replies" do+ Fabricate(:post, topic:, user: Fabricate(:user))+get "/t/#{topic.slug}/#{topic.id}", env: crawler_envdoc = parsed_crawler_body@@ -114,9 +115,37 @@ RSpec.describe TopicsController doexpect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(0)end+ it "keeps DiscussionForumPosting for single-post topics" do+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env+ doc = parsed_crawler_body++ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(0)+ end++ it "keeps DiscussionForumPosting when only replies are hidden" do+ Fabricate(:post, topic:, hidden: true)++ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env+ doc = parsed_crawler_body++ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(0)+ end++ it "keeps DiscussionForumPosting when only replies are small action posts" do+ Fabricate(:post, topic:, post_type: Post.types[:small_action])++ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env+ doc = parsed_crawler_body++ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(0)+ end+it "marks the solution post as acceptedAnswer and other replies as suggestedAnswer" do- p3 = Fabricate(:post, topic: topic, user: Fabricate(:user))- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ p3 = Fabricate(:post, topic:, user: Fabricate(:user))+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}", env: crawler_envdoc = parsed_crawler_body@@ -158,7 +187,7 @@ RSpec.describe TopicsController doit "emits QAPage schema when set to 'answered only' with an accepted answer" doSiteSetting.solved_add_schema_markup = "answered only"- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}", env: crawler_envdoc = parsed_crawler_body@@ -175,22 +204,22 @@ RSpec.describe TopicsController doit "includes the correct schema information" doDiscourseTagging.add_or_create_tags_by_name(topic, [tag.name])- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}"- expect(response.body).to include(schema_json(1))+ expect(response.body).to include(expected_schema_json)endit "doesn't include solved schema information when the topic has a different tag" doanother_tag = Fabricate(:tag)DiscourseTagging.add_or_create_tags_by_name(topic, [another_tag.name])- Fabricate(:solved_topic, topic: topic, answer_post: p2)+ Fabricate(:solved_topic, topic:, answer_post: p2)get "/t/#{topic.slug}/#{topic.id}"- expect(response.body).not_to include(schema_json(1))+ expect(response.body).not_to include(expected_schema_json)endendenddiff --git a/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb b/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rbindex 513b33cc46f..06d0293f643 100644--- a/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb+++ b/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb@@ -9,11 +9,11 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup dosubject(:result) { described_class.call(params:, **dependencies) }fab!(:user)- fab!(:topic) { Fabricate(:topic, user: user) }- fab!(:post) { Fabricate(:post, topic: topic, user: user, like_count: 1) }+ fab!(:topic) { Fabricate(:topic, user:) }+ fab!(:post) { Fabricate(:post, topic:, user:, like_count: 1) }let(:guardian) { Guardian.new(user) }let(:params) { { topic_id: topic.id } }- let(:dependencies) { { guardian: guardian } }+ let(:dependencies) { { guardian: } }before { SiteSetting.allow_solved_on_all_topics = true }@@ -41,22 +41,29 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup doit { is_expected.to fail_a_policy(:schema_markup_enabled) }end- context "when setting is 'always' and there is no accepted answer" do+ context "when setting is 'always' and topic has no answers" dobefore { SiteSetting.solved_add_schema_markup = "always" }- it { is_expected.to run_successfully }+ it { is_expected.to fail_a_policy(:has_answers) }- it "builds QAPage markup without an answer" do- html = result[:html]- expect(html).to include("application/ld+json")- expect(html).to include('"@type":"QAPage"')- expect(html).to include('"answerCount":0')- expect(html).not_to include('"acceptedAnswer"')- expect(html).not_to include('"suggestedAnswer"')+ context "when the only reply is a small action post" do+ fab!(:small_action) { Fabricate(:post, topic:, post_type: Post.types[:small_action]) }++ it { is_expected.to fail_a_policy(:has_answers) }end+ context "when the only reply is hidden" do+ fab!(:hidden_reply) { Fabricate(:post, topic:, hidden: true) }++ it { is_expected.to fail_a_policy(:has_answers) }+ end+ end++ context "when setting is 'always' and there is no accepted answer" do+ before { SiteSetting.solved_add_schema_markup = "always" }+context "when the topic has replies" do- fab!(:reply) { Fabricate(:post, topic: topic) }+ fab!(:reply) { Fabricate(:post, topic:) }it "includes replies as suggested answers" dohtml = result[:html]@@ -67,8 +74,8 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup doendcontext "when the topic has hidden replies" do- fab!(:hidden_reply) { Fabricate(:post, topic: topic, hidden: true) }- fab!(:visible_reply) { Fabricate(:post, topic: topic) }+ fab!(:hidden_reply) { Fabricate(:post, topic:, hidden: true) }+ fab!(:visible_reply) { Fabricate(:post, topic:) }it "excludes hidden posts from suggested answers" dohtml = result[:html]@@ -81,11 +88,11 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup docontext "when there is an accepted answer but no other answers" dofab!(:answer_user, :user)- fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user, like_count: 3) }+ fab!(:answer_post) { Fabricate(:post, topic:, user: answer_user, like_count: 3) }before doSiteSetting.solved_add_schema_markup = "always"- Fabricate(:solved_topic, topic: topic, answer_post: answer_post)+ Fabricate(:solved_topic, topic:, answer_post:)endit { is_expected.to run_successfully }@@ -101,15 +108,45 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup doendend+ context "when the accepted answer is hidden but there are other visible replies" do+ fab!(:answer_user, :user)+ fab!(:answer_post) { Fabricate(:post, topic:, user: answer_user, like_count: 3) }+ fab!(:visible_reply) { Fabricate(:post, topic:) }++ before do+ SiteSetting.solved_add_schema_markup = "always"+ Fabricate(:solved_topic, topic:, answer_post:)+ answer_post.update!(hidden: true)+ end++ it "excludes the hidden accepted answer but includes suggested answers" do+ html = result[:html]+ expect(html).not_to include('"acceptedAnswer"')+ expect(html).to include('"suggestedAnswer"')+ end+ end++ context "when the accepted answer is hidden and there are no other replies" do+ fab!(:answer_post) { Fabricate(:post, topic:) }++ before do+ SiteSetting.solved_add_schema_markup = "always"+ Fabricate(:solved_topic, topic:, answer_post:)+ answer_post.update!(hidden: true)+ end++ it { is_expected.to fail_a_policy(:has_answers) }+ end+context "when there is an accepted answer and suggested answers" dofab!(:answer_user, :user)- fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user, like_count: 3) }+ fab!(:answer_post) { Fabricate(:post, topic:, user: answer_user, like_count: 3) }fab!(:suggested_user, :user)- fab!(:suggested_post) { Fabricate(:post, topic: topic, user: suggested_user, like_count: 1) }+ fab!(:suggested_post) { Fabricate(:post, topic:, user: suggested_user, like_count: 1) }before doSiteSetting.solved_add_schema_markup = "always"- Fabricate(:solved_topic, topic: topic, answer_post: answer_post)+ Fabricate(:solved_topic, topic:, answer_post:)endit { is_expected.to run_successfully }