1 # frozen_string_literal: true
 
   3 require "application_system_test_case"
 
   5 class DiaryEntrySystemTest < ApplicationSystemTestCase
 
   7     create(:language, :code => "en")
 
   8     create(:language, :code => "pt", :english_name => "Portuguese", :native_name => "Português")
 
   9     create(:language, :code => "pt-BR", :english_name => "Brazilian Portuguese", :native_name => "Português do Brasil")
 
  10     create(:language, :code => "ru", :english_name => "Russian", :native_name => "Русский")
 
  11     @diary_entry = create(:diary_entry)
 
  14   test "reply to diary entry should prefill the message subject" do
 
  15     sign_in_as(create(:user))
 
  16     visit diary_entries_path
 
  18     click_on "Send a message to the author"
 
  20     assert_content "Send a new message"
 
  21     assert_equal "Re: #{@diary_entry.title}", page.find_field("Subject").value
 
  24   test "deleted diary entries should be hidden for regular users" do
 
  25     @deleted_entry = create(:diary_entry, :visible => false)
 
  27     sign_in_as(create(:user))
 
  28     visit diary_entries_path
 
  30     assert_no_content @deleted_entry.title
 
  33   test "deleted diary entries should be shown to administrators for review" do
 
  34     @deleted_entry = create(:diary_entry, :visible => false)
 
  36     sign_in_as(create(:administrator_user))
 
  37     visit diary_entries_path
 
  39     assert_content @deleted_entry.title
 
  42   test "deleted diary entries should not be shown to admins when the user is also deleted" do
 
  43     @deleted_user = create(:user, :deleted)
 
  44     @deleted_entry = create(:diary_entry, :visible => false, :user => @deleted_user)
 
  46     sign_in_as(create(:administrator_user))
 
  47     visit diary_entries_path
 
  49     assert_no_content @deleted_entry.title
 
  52   test "deleted diary comments should be hidden for regular users" do
 
  53     @deleted_comment = create(:diary_comment, :diary_entry => @diary_entry, :visible => false)
 
  55     sign_in_as(create(:user))
 
  56     visit diary_entry_path(@diary_entry.user, @diary_entry)
 
  58     assert_no_content @deleted_comment.body
 
  61   test "deleted diary comments should be shown to administrators" do
 
  62     @deleted_comment = create(:diary_comment, :diary_entry => @diary_entry, :visible => false)
 
  64     sign_in_as(create(:administrator_user))
 
  65     visit diary_entry_path(@diary_entry.user, @diary_entry)
 
  67     assert_content @deleted_comment.body
 
  70   test "should have links to preferred languages" do
 
  71     sign_in_as(create(:user, :languages => %w[en-US pt-BR]))
 
  72     visit diary_entries_path
 
  74     assert_link "Diary Entries in English", :href => "/diary/en"
 
  75     assert_link "Diary Entries in Brazilian Portuguese", :href => "/diary/pt-BR"
 
  76     assert_link "Diary Entries in Portuguese", :href => "/diary/pt"
 
  77     assert_no_link "Diary Entries in Russian"
 
  80   test "should have new diary entry link on own diary entry page" do
 
  82     diary_entry = create(:diary_entry, :user => user)
 
  85     visit diary_entry_path(diary_entry.user, diary_entry)
 
  87     within_content_heading do
 
  88       assert_link "New Diary Entry"
 
  92   test "should not have new diary entry link on other user's diary entry page" do
 
  94     diary_entry = create(:diary_entry)
 
  97     visit diary_entry_path(diary_entry.user, diary_entry)
 
  99     within_content_heading do
 
 100       assert_no_link "New Diary Entry"
 
 104   test "should not be hidden on the list page" do
 
 105     body = SecureRandom.alphanumeric(1998)
 
 106     create(:diary_entry, :body => body)
 
 108     visit diary_entries_path
 
 111     assert_no_content I18n.t("diary_entries.diary_entry.full_entry")
 
 114   test "should be hidden on the list page" do
 
 115     body = SecureRandom.alphanumeric(2000)
 
 116     create(:diary_entry, :body => body)
 
 118     visit diary_entries_path
 
 120     assert_no_content body
 
 121     assert_content I18n.t("diary_entries.diary_entry.full_entry")
 
 124   test "should be partially hidden on the list page" do
 
 131     body = "<p>#{text1}</p><div><p>#{text2}</p><p>#{text3}<a href='#'>#{text4}</a></p></div><p>#{text5}</p>"
 
 132     create(:diary_entry, :body => body)
 
 134     visit diary_entries_path
 
 138     assert_no_content text3
 
 139     assert_no_content text4
 
 140     assert_no_content text5
 
 141     assert_content I18n.t("diary_entries.diary_entry.full_entry")
 
 144   test "should not be hidden on the show page" do
 
 145     body = SecureRandom.alphanumeric(2001)
 
 146     diary_entry = create(:diary_entry, :body => body)
 
 148     visit diary_entry_path(diary_entry.user, diary_entry)
 
 151     assert_no_content I18n.t("diary_entries.diary_entry.full_entry")
 
 154   test "contents after diary entry should be below floated images" do
 
 156     diary_entry = create(:diary_entry, :user => user, :body => "<img width=100 height=1000 align=left alt='Floated Image'>")
 
 159     visit diary_entry_path(user, diary_entry)
 
 161     img = find "img[alt='Floated Image']"
 
 162     assert_link "Edit this entry", :below => img