Skip to content

Commit

Permalink
Journalize the deletion of Wiki Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Yalaeddin authored Sep 27, 2023
1 parent 73ebf3b commit e876d1d
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def add_journal_entry_for_user(user:, property:, prop_key:, value: nil, old_valu
old_value: old_value), author: author
end

def add_wiki_page_journal_entry(project:, value: nil, old_value: nil)
add_journal_entry project, JournalDetail.new(
property: 'wiki_page',
prop_key: 'wiki_page',
value: value,
old_value: old_value)
end

def add_member_creation_to_journal(member, role_ids, function_ids = nil)
prop_key = limited_visibility_plugin_installed? ? 'member_roles_and_functions' : 'member_with_roles'
add_member_journal_entry(project: member.project, property: 'members', prop_key: prop_key, value: value_hash(member, role_ids, function_ids))
Expand Down
25 changes: 25 additions & 0 deletions lib/redmine_admin_activity/controllers/wiki_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_dependency 'wiki_controller'

class WikiController
include RedmineAdminActivity::Journalizable

after_action :journalized_wiki_page_deletion, :only => [:destroy]
before_action :self_and_descendants, :only => [:destroy]

def self_and_descendants
@self_and_descendants = [@page] # case of leaf page, or page without children, or page with children and params[:todo] = nullify or reassign
@self_and_descendants += @page.descendants.to_a if params[:todo].present? && params[:todo] == 'destroy'
end

def journalized_wiki_page_deletion
return unless @page.present? && @page.destroyed?

@self_and_descendants.each do |wiki|

add_wiki_page_journal_entry(project: wiki.project,
value: nil,
old_value: wiki.title)
end
end

end
7 changes: 7 additions & 0 deletions lib/redmine_admin_activity/helpers/issues_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def show_detail(detail, no_html = false, options = {})
when 'members_exception'
str = detail.prop_key.delete_prefix! "members_exception_with_"
show_members_exception_details(detail, str, options)
when 'wiki_page'
show_label_property_details(detail, no_html, options)
else
if detail.property != 'cf' && detail.journal.present? && detail.journal.journalized_type == 'Principal'
details = show_principal_detail(detail, no_html, options)
Expand Down Expand Up @@ -195,6 +197,11 @@ def show_user_creation_details(detail, no_html, options = {})
l(:text_journal_create_user_journal_entry, :label => label).html_safe
end

def show_label_property_details(detail, no_html, options = {})
label_property = "label_#{detail.property}"
l(:text_journal_deleted, :label => l(label_property), :old => detail.old_value).html_safe
end

def get_project_status_label_for_history
{
Project::STATUS_ACTIVE.to_s => l(:project_status_active),
Expand Down
1 change: 1 addition & 0 deletions lib/redmine_admin_activity/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def after_plugins_loaded(_context = {})
require_relative 'controllers/custom_fields_controller'
require_relative 'controllers/settings_controller'
require_relative 'controllers/custom_field_enumerations_controller'
require_relative 'controllers/wiki_controller'

if Redmine::Plugin.installed?(:redmine_organizations)
require_relative 'controllers/organizations/memberships_controller'
Expand Down
75 changes: 75 additions & 0 deletions spec/controllers/wiki_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'spec_helper'

describe WikiController, type: :controller do

render_views

fixtures :projects, :users, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :journals, :journal_details

include Redmine::I18n

before do
@controller = WikiController.new
@request = ActionDispatch::TestRequest.create
@response = ActionDispatch::TestResponse.new
User.current = nil
@request.session[:user_id] = 1 #permissions are hard
end

let(:project) { projects(:projects_001) }
let(:parent_page) { wiki_pages(:wiki_pages_002) }

describe "DELETE destroy, logs change on Journal and JournalDetail" do

it "When we delete a page without children" do
wiki_title = WikiPage.find(6).title

expect do
delete :destroy, :params => { :project_id => project.id, :id => wiki_title }
end.to change { Journal.count }.by(1)
.and change { JournalDetail.count }.by(1)
.and change { WikiPage.count }.by(-1)

expect(JournalDetail.last.old_value).to eq(wiki_title)
expect(JournalDetail.last.prop_key).to eq("wiki_page")
expect(JournalDetail.last.property).to eq("wiki_page")
expect(Journal.last.journalized).to eq(project)
end

it "When we delete a parent page with option nullify" do
expect do
delete :destroy, :params => {:project_id => project.id, :id => parent_page.title, :todo => 'nullify'}
end.to change { Journal.count }.by(1)
.and change { JournalDetail.count }.by(1)
.and change { WikiPage.count }.by(-1)

expect(JournalDetail.last.old_value).to eq(parent_page.title)
expect(JournalDetail.last.prop_key).to eq("wiki_page")
expect(JournalDetail.last.property).to eq("wiki_page")
expect(Journal.last.journalized).to eq(project)
end

it "When we delete a parent page with option destroy cascade" do
total_size = parent_page.descendants.size + 1

expect do
delete :destroy, :params => {:project_id => project.id, :id => parent_page.title, :todo => 'destroy'}
end.to change { Journal.count }.by(total_size)
.and change { JournalDetail.count }.by(total_size)
.and change { WikiPage.count }.by(-total_size)
end
it "When we delete a parent page with option reassign" do
expect do
delete :destroy, :params => {:project_id => project.id, :id => parent_page.title, :todo => 'reassign', :reassign_to_id => 1 }
end.to change { Journal.count }.by(1)
.and change { JournalDetail.count }.by(1)
.and change { WikiPage.count }.by(-1)

expect(JournalDetail.last.old_value).to eq(parent_page.title)
expect(JournalDetail.last.prop_key).to eq("wiki_page")
expect(JournalDetail.last.property).to eq("wiki_page")
expect(Journal.last.journalized).to eq(project)
end
end

end
7 changes: 7 additions & 0 deletions spec/helpers/issues_helper_patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,11 @@
detail = JournalDetail.new(:journal => journal, :property => 'creation', :value => User::USER_AUTO_CREATION, :prop_key => 'creation')
expect(show_detail(detail, true)).to eq "This user has been created automatically."
end

it "shoud IssueHelper#show_detail should label property" do
page_title = 'page test'
detail = JournalDetail.new(:property => 'wiki_page', :old_value => page_title, :prop_key => 'wiki_page')
expect(show_detail(detail, true)).to eq "Wiki page deleted (#{page_title})"
end

end

0 comments on commit e876d1d

Please sign in to comment.