require_relative '../test_helper' require 'benchmark' class ArticleTest < ActiveSupport::TestCase def setup @profile = fast_create(Community) @article = fast_create(TextArticle, :profile_id => profile.id) @environment = Environment.default @environment.enable_plugin(CommentParagraphPlugin) end attr_reader :article, :environment, :profile should 'return paragraph comments from article' do comment1 = fast_create(Comment, :paragraph_uuid => 1, :source_id => article.id) comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) assert_equal [comment1], article.paragraph_comments end should 'allow save if comment paragraph macro is not removed for paragraph with comments' do article.body = "
" comment1 = fast_create(Comment, :paragraph_uuid => 0, :source_id => article.id) assert article.save end should 'not parse html if the plugin is not enabled' do article.body = "paragraph 1
paragraph 2
" environment.disable_plugin(CommentParagraphPlugin) assert !environment.plugin_enabled?(CommentParagraphPlugin) article.save! assert_equal "paragraph 1
paragraph 2
", article.body end should 'parse html if the plugin is not enabled' do article.body = "paragraph 1
paragraph 1
" article.comment_paragraph_plugin_activate = true article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' article.comment_paragraph_plugin_activate = false article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' end should 'parse html when activate comment paragraph' do article.body = "paragraph 1
paragraph 2
" article.comment_paragraph_plugin_activate = false article.save! assert_equal "paragraph 1
paragraph 2
", article.body article.comment_paragraph_plugin_activate = true article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' assert_mark_paragraph article.body, 'p', 'paragraph 2' end should 'parse html when add new paragraph' do article.body = "paragraph 1
" article.comment_paragraph_plugin_activate = true article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' article.body += "paragraph 2
" article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' assert_mark_paragraph article.body, 'p', 'paragraph 2' end should 'keep already marked paragraph attributes when add new paragraph' do article.body = "paragraph 1
" article.comment_paragraph_plugin_activate = true article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid'] article.body += "paragraph 2
" article.save! assert_mark_paragraph article.body, 'p', 'paragraph 1' new_uuid = Nokogiri::HTML(article.body).at('p span.paragraph_comment')['data-macro-paragraph_uuid'] assert_equal uuid, new_uuid end should 'not parse empty element' do article.body = '' article.comment_paragraph_plugin_activate = true article.save! assert_equal '', article.body end should 'be enabled if plugin is enabled and article is a kind of TextArticle' do assert article.comment_paragraph_plugin_enabled? end should 'not be enabled if plugin is not enabled' do environment.disable_plugin(CommentParagraphPlugin) assert !article.comment_paragraph_plugin_enabled? end should 'not be enabled if article if not a kind of TextArticle' do article = fast_create(Article, :profile_id => profile.id) assert !article.comment_paragraph_plugin_enabled? end should 'not be activated by default' do article = fast_create(TextArticle, :profile_id => profile.id) assert !article.comment_paragraph_plugin_activated? end should 'be activated by default if it is enabled and activation mode is auto' do settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin) settings.activation_mode = 'auto' settings.save! article = TextArticle.create!(:profile => profile, :name => 'title') assert article.comment_paragraph_plugin_activated? end should 'be activated when forced' do article.comment_paragraph_plugin_activate = true assert article.comment_paragraph_plugin_activated? end should 'not be activated if plugin is not enabled' do article.comment_paragraph_plugin_activate = true environment.disable_plugin(CommentParagraphPlugin) assert !article.comment_paragraph_plugin_enabled? end should 'append not_logged to cache key when user is not logged in' do assert_match /-not_logged-/, article.cache_key end should 'append logged_in to cache key when user is logged in' do assert_match /-logged_in-/, article.cache_key({}, fast_create(Person)) end should 'return paragraph content passing paragraph uuid' do uuid = 0 article.body = "