#218 Recycle bin - unit tests

This commit is contained in:
Karel Pičman 2014-05-30 14:42:25 +02:00
parent 0ef11f76af
commit 8d3c66c097
15 changed files with 399 additions and 100 deletions

View File

@ -333,7 +333,7 @@ class DmsfController < ApplicationController
if @folder.delete(commit)
flash[:notice] = l(:notice_folder_deleted)
else
flash[:error] = folder.errors[:base][0]
flash[:error] = @folder.errors[:base][0]
end
if commit
redirect_to :back

View File

@ -20,7 +20,7 @@ class DmsfLinksController < ApplicationController
unloadable
model_object DmsfLink
before_filter :find_model_object, :only => [:destroy]
before_filter :find_model_object, :only => [:destroy, :restore]
before_filter :find_link_project
before_filter :authorize

View File

@ -30,6 +30,7 @@ class DmsfFileRevision < ActiveRecord::Base
# Returns a list of revisions that are not deleted here, or deleted at parent level either
scope :visible, where('NOT deleted')
scope :deleted, where('NOT NOT deleted')
acts_as_customizable
acts_as_event :title => Proc.new {|o| "#{l(:label_dmsf_updated)}: #{o.file.dmsf_path_str}"},

View File

@ -69,7 +69,7 @@ Redmine::Plugin.register :redmine_dmsf do
{:dmsf_files => [:create_revision, :lock, :unlock, :delete_revision, :notify_activate, :notify_deactivate, :restore],
:dmsf_upload => [:upload_files, :upload_file, :commit_files],
:dmsf_workflows => [:action, :new_action, :autocomplete_for_user, :start, :assign, :assignment],
:dmsf_links => [:new, :create, :destroy]
:dmsf_links => [:new, :create, :destroy, :restore]
}
permission :file_delete, { :dmsf => [:trash], :dmsf_files => [:delete]}
permission :manage_workflows,

View File

@ -1,48 +1,48 @@
---
dmsf_file_revisions_001:
id: 1
dmsf_file_id: 1
source_dmsf_file_revision_id: NULL
name: "test.txt"
dmsf_folder_id: NULL
disk_filename: "test.txt"
size: 4
mime_type: text/plain
title: "Test File"
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
id: 1
dmsf_file_id: 1
source_dmsf_file_revision_id: NULL
name: "test.txt"
dmsf_folder_id: NULL
disk_filename: "test.txt"
size: 4
mime_type: text/plain
title: "Test File"
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
dmsf_workflow_assigned_by: 1
dmsf_workflow_started_by: 1
project_id: 1
project_id: 1
#revision for file on non-enabled project
dmsf_file_revisions_002:
id: 2
dmsf_file_id: 2
source_dmsf_file_revision_id: NULL
name: "test.txt"
dmsf_folder_id: NULL
disk_filename: "test.txt"
size: 4
mime_type: text/plain
title: "Test File"
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
id: 2
dmsf_file_id: 2
source_dmsf_file_revision_id: NULL
name: "test.txt"
dmsf_folder_id: NULL
disk_filename: "test.txt"
size: 4
mime_type: text/plain
title: "Test File"
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
dmsf_workflow_assigned_by: 1
dmsf_workflow_started_by: 1
project_id: 2
project_id: 2
#revision for deleted file on dmsf-enabled project
dmsf_file_revisions_003:
@ -65,4 +65,48 @@ dmsf_file_revisions_003:
user_id: 1
dmsf_workflow_assigned_by: 1
dmsf_workflow_started_by: 1
project_id: 1
project_id: 1
dmsf_file_revisions_004:
id: 4
dmsf_file_id: 4
source_dmsf_file_revision_id: NULL
name: 'test.txt'
dmsf_folder_id: NULL
disk_filename: 'test.txt'
size: 4
mime_type: text/plain
title: 'Test File'
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
dmsf_workflow_assigned_by: NULL
dmsf_workflow_started_by: NULL
project_id: 1
dmsf_file_revisions_005:
id: 5
dmsf_file_id: 1
source_dmsf_file_revision_id: NULL
name: "test.txt"
dmsf_folder_id: NULL
disk_filename: "test.txt"
size: 4
mime_type: text/plain
title: "Test File"
description: NULL
workflow: NULL
minor_version: 0
major_version: 1
comment: NULL
deleted: 0
deleted_by_user_id: NULL
user_id: 1
dmsf_workflow_assigned_by: NULL
dmsf_workflow_started_by: NULL
project_id: 1

View File

@ -12,6 +12,19 @@ folder_link:
created_at: <%= DateTime.now() %>
updated_at: <%= DateTime.now() %>
folder_link2:
id: 3
target_project_id: 2
target_id: 4
target_type: DmsfFolder
name: folder4_link
project_id: 2
dmsf_folder_id: NULL
deleted: 0
deleted_by_user_id: NULL
created_at: <%= DateTime.now() %>
updated_at: <%= DateTime.now() %>
file_link:
id: 2
target_project_id: 1

View File

@ -19,6 +19,7 @@
require File.expand_path('../../test_helper', __FILE__)
class DmsfControllerTest < RedmineDmsf::Test::TestCase
include Redmine::I18n
fixtures :users, :dmsf_folders, :custom_fields, :custom_values, :projects,
:roles, :members, :member_roles
@ -28,31 +29,91 @@ class DmsfControllerTest < RedmineDmsf::Test::TestCase
@project = Project.find_by_id 1
assert_not_nil @project
@project.enable_module! :dmsf
@folder = DmsfFolder.find_by_id 1
@folder1 = DmsfFolder.find_by_id 1
@folder2 = DmsfFolder.find_by_id 2
@folder4 = DmsfFolder.find_by_id 4
@role = Role.find_by_id 1
@custom_field = CustomField.find_by_id 21
@custom_value = CustomValue.find_by_id 21
@user1 = User.find_by_id 1
User.current = @user1
end
def test_truth
assert_kind_of Project, @project
assert_kind_of DmsfFolder, @folder
assert_kind_of DmsfFolder, @folder1
assert_kind_of DmsfFolder, @folder2
assert_kind_of DmsfFolder, @folder4
assert_kind_of Role, @role
assert_kind_of CustomField, @custom_field
assert_kind_of CustomValue, @custom_value
assert_kind_of User, @user1
end
def test_edit_folder
# Missing permissions
get :edit, :id => @project, :folder_id => @folder
get :edit, :id => @project, :folder_id => @folder1
assert_response 403
# Permissions OK
@role.add_permission! :view_dmsf_folders
@role.add_permission! :folder_manipulation
get :edit, :id => @project, :folder_id => @folder
get :edit, :id => @project, :folder_id => @folder1
assert_response :success
assert_select 'label', { :text => @custom_field.name }
assert_select 'option', { :value => @custom_value.value }
end
def test_trash
# Missing permissions
get :trash, :id => @project
assert_response 403
# Permissions OK
@role.add_permission! :file_delete
get :trash, :id => @project
assert_response :success
assert_select 'h2', { :text => l(:link_trash_bin) }
end
def test_delete
# Missing permissions
get :delete, :id => @project, :folder_id => @folder1.id, :commit => false
assert_response 403
# Permissions OK but the folder is not empty
@role.add_permission! :folder_manipulation
get :delete, :id => @project, :folder_id => @folder1.id, :commit => false
assert_response :redirect
assert_include l(:error_folder_is_not_empty), flash[:error]
# Permissions OK but the folder is locked
get :delete, :id => @project, :folder_id => @folder2.id, :commit => false
assert_response :redirect
assert_include l(:error_folder_is_locked), flash[:error]
# Empty and not locked folder
flash[:error].clear
get :delete, :id => @project, :folder_id => @folder4.id, :commit => false
assert_response :redirect
assert_equal 0, flash[:error].size
end
def test_restore
@role.add_permission! :folder_manipulation
get :delete, :id => @project, :folder_id => @folder4.id
assert_response :redirect
# Missing permissions
@role.remove_permission! :folder_manipulation
get :restore, :id => @project, :folder_id => @folder4.id
assert_response 403
# Permissions OK
@request.env['HTTP_REFERER'] = trash_dmsf_path(:id => @project.id)
@role.add_permission! :folder_manipulation
get :restore, :id => @project, :folder_id => @folder4.id
assert_response :redirect
end
end

View File

@ -1,6 +1,6 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2013 Karel Pičman <karel.picman@kontron.com>
# Copyright (C) 2011-14 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -66,5 +66,25 @@ class DmsfFilesControllerTest < RedmineDmsf::Test::TestCase
# assert_select 'label', { :text => @custom_field.name }
# assert_select 'option', { :value => @custom_value.value }
# end
def delete
# Missing permissions
delete @file, :commit => false
assert_response 403
# Permissions OK but the file is locked
@role.add_permission! :file_delete
delete @file, :commit => false
assert_response :redirect
assert_include l(:error_file_is_locked), flash[:error]
# Permissions OK and not locked
flash[:error].clear
@file.unlock!
delete @file, :commit => false
assert_response :redirect
assert_equal 0, flash[:error].size
end
end

View File

@ -1,6 +1,6 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2014 Karel Pičman <karel.picman@lbcfree.net>
# Copyright (C) 2011-14 Karel Pičman <karel.picman@lbcfree.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -297,4 +297,19 @@ class DmsfLinksControllerTest < RedmineDmsf::Test::TestCase
end
assert_redirected_to dmsf_folder_path(:id => @project1.id, :folder_id => @folder1.id)
end
def test_restore
#User.current = @user_admin
@request.env['HTTP_REFERER'] = trash_dmsf_path(:id => @project1.id)
# Missing permissions
@role_manager.remove_permission! :file_manipulation
get :restore, :project_id => @project1.id, :id => @file_link.id
assert_response :forbidden
# Permissions OK
@role_manager.add_permission! :file_manipulation
get :restore, :project_id => @project1.id, :id => @file_link.id
assert_response :redirect
end
end

View File

@ -0,0 +1,48 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2011-14 Karel Picman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../test_helper', __FILE__)
class DmsfFileRevisionTest < RedmineDmsf::Test::UnitTest
fixtures :projects, :users, :dmsf_folders, :dmsf_files, :dmsf_file_revisions,
:roles, :members, :member_roles, :enabled_modules, :enumerations,
:dmsf_locks
def setup
@revision5 = DmsfFileRevision.find_by_id 5
end
def test_truth
assert_kind_of DmsfFileRevision, @revision5
end
def test_delete_restore
@revision5.delete false
assert_nil DmsfFileRevision.visible.where(:id => @revision5.id).first
assert DmsfFileRevision.deleted.where(:id => @revision5.id).first
@revision5.restore
assert_nil DmsfFileRevision.deleted.where(:id => @revision5.id).first
assert DmsfFileRevision.visible.where(:id => @revision5.id).first
end
def test_destroy
@revision5.delete true
assert_nil DmsfFileRevision.find_by_id @revision5.id
end
end

View File

@ -1,6 +1,7 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-14 Karel Picman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -9,71 +10,105 @@
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../test_helper', __FILE__)
class DmsfFileTest < RedmineDmsf::Test::UnitTest
fixtures :projects, :users, :dmsf_folders, :dmsf_files, :dmsf_file_revisions,
:roles, :members, :member_roles, :enabled_modules, :enumerations,
:dmsf_locks
:roles, :members, :member_roles, :dmsf_locks, :dmsf_links
def setup
@user1 = User.find_by_id 1
@project1 = Project.find_by_id 1
@file1 = DmsfFile.find_by_id 1
@file2 = DmsfFile.find_by_id 2
@file3 = DmsfFile.find_by_id 3
@file4 = DmsfFile.find_by_id 4
@file5 = DmsfFile.find_by_id 5
end
def test_truth
assert_kind_of User, @user1
assert_kind_of Project, @project1
assert_kind_of DmsfFile, @file1
assert_kind_of DmsfFile, @file2
assert_kind_of DmsfFile, @file3
assert_kind_of DmsfFile, @file4
assert_kind_of DmsfFile, @file5
end
test "file data is created" do
assert_not_nil(dmsf_files(:dmsf_files_001))
test "project file count differs from project visibility count" do
assert_not_same(@project1.dmsf_files.count, @project1.dmsf_files.visible.count)
end
test "project file count differs from project visibility count" do
project = Project.find(1)
assert_not_same(project.dmsf_files.count, project.dmsf_files.visible.count)
end
test "project DMSF file listing contains deleted items" do
project = Project.find(1)
test "project DMSF file listing contains deleted items" do
found_deleted = false
project.dmsf_files.each {|file|
@project1.dmsf_files.each {|file|
found_deleted = true if file.deleted
}
assert found_deleted, "Expected at least one deleted item in <all items>"
end
test "project DMSF file visible listing contains no deleted items" do
project = Project.find(1)
project.dmsf_files.visible.each {|file|
test "project DMSF file visible listing contains no deleted items" do
@project1.dmsf_files.visible.each {|file|
assert !file.deleted, "File #{file.name} is deleted, this was unexpected"
}
end
test "Known locked file responds as being locked" do
file = dmsf_files(:dmsf_files_002)
assert file.locked?
test "Known locked file responds as being locked" do
assert @file2.locked?
end
test "File with locked folder is reported as locked" do
file = dmsf_files(:dmsf_files_004)
assert file.locked?
test "File with locked folder is reported as locked" do
assert @file4.locked?
end
test "File with folder up heirarchy (locked) is reported as locked" do
file = dmsf_files(:dmsf_files_005)
assert file.locked?
test "File with folder up heirarchy (locked) is reported as locked" do
assert @file5.locked?
end
test "File with folder up heirarchy (locked) is not locked for user id 1" do
User.current = User.find(1)
file = dmsf_files(:dmsf_files_005)
assert file.locked?
assert !file.locked_for_user?
User.current = @user1
assert @file5.locked?
assert !@file5.locked_for_user?
end
test "File with no locks reported unlocked" do
file = dmsf_files(:dmsf_files_001)
assert !file.locked?
test "File with no locks reported unlocked" do
assert !@file1.locked?
end
def test_delete_restore
assert_equal 1, @file4.revisions.visible.count
assert_equal 1, @file4.referenced_links.visible.count
# Delete
@file4.delete false
assert_nil DmsfFile.visible.where(:id => @file4.id).first
assert DmsfFile.deleted.where(:id => @file4.id).first
assert_equal 0, @file4.revisions.visible.count
assert_equal 0, @file4.referenced_links.visible.count
# Restore
@file4.restore
assert_nil DmsfFile.deleted.where(:id => @file4.id).first
assert DmsfFile.visible.where(:id => @file4.id).first
assert_equal 1, @file4.revisions.visible.count
assert_equal 1, @file4.referenced_links.visible.count
end
def test_destroy
assert_equal 1, @file4.revisions.visible.count
assert_equal 1, @file4.referenced_links.visible.count
@file4.delete true
assert_nil DmsfFile.find_by_id(@file4.id)
assert_equal 0, @file4.revisions.count
assert_equal 0, @file4.referenced_links.count
end
end
end

View File

@ -1,6 +1,7 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-14 Karel Picman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -16,14 +17,37 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../test_helper.rb', __FILE__)
require File.expand_path('../../test_helper', __FILE__)
class DmsfFolderTest < RedmineDmsf::Test::UnitTest
fixtures :projects, :users, :dmsf_folders, :dmsf_files, :dmsf_file_revisions,
:roles, :members, :member_roles, :enabled_modules, :enumerations
def test_folder_creating
assert_not_nil(dmsf_folders(:dmsf_folders_001))
:roles, :members, :member_roles, :dmsf_locks, :dmsf_links
def setup
@folder4 = DmsfFolder.find_by_id 4
end
end
def test_truth
assert_kind_of DmsfFolder, @folder4
end
def test_delete_restore
assert_equal 1, @folder4.referenced_links.visible.count
@folder4.delete false
assert_nil DmsfFolder.visible.where(:id => @folder4.id).first
assert DmsfFolder.deleted.where(:id => @folder4.id).first
assert_equal 0, @folder4.referenced_links.visible.count
@folder4.restore
assert_nil DmsfFolder.deleted.where(:id => @folder4.id).first
assert DmsfFolder.visible.where(:id => @folder4.id).first
assert_equal 1, @folder4.referenced_links.visible.count
end
def test_destroy
assert_equal 1, @folder4.referenced_links.visible.count
@folder4.delete true
assert_nil DmsfFolder.find_by_id(@folder4.id)
assert_equal 0, DmsfLink.where(:target_id => @folder4.id, :target_type => DmsfFolder.model_name).count
end
end

View File

@ -1,6 +1,6 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2014 Karel Pičman <karel.picman@lbcfree.net>
# Copyright (C) 2011-14 Karel Pičman <karel.picman@lbcfree.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -138,10 +138,8 @@ class DmsfLinksTest < RedmineDmsf::Test::UnitTest
end
def test_path
assert_equal @file_link.path,
@file_link.target_file.dmsf_path_str
assert_equal @folder_link.path,
@folder_link.target_folder.dmsf_path_str
assert_equal 'folder1/folder2/test.txt', @file_link.path
assert_equal 'folder1', @folder_link.path
end
def test_copy_to
@ -166,9 +164,32 @@ class DmsfLinksTest < RedmineDmsf::Test::UnitTest
assert_equal folder_link_copy.dmsf_folder_id, @folder2.id
end
def test_destroy
@folder_link.destroy
assert_nil DmsfLink.find_by_id 1
def test_delete_restore
# File link
@file_link.delete false
assert_nil DmsfLink.visible.where(:id => @file_link.id).first
assert DmsfLink.deleted.where(:id => @file_link.id).first
@file_link.restore
assert_nil DmsfLink.deleted.where(:id => @file_link.id).first
assert DmsfLink.visible.where(:id => @file_link.id).first
# Folder link
@folder_link.delete false
assert_nil DmsfLink.visible.where(:id => @folder_link.id).first
assert DmsfLink.deleted.where(:id => @folder_link.id).first
@folder_link.restore
assert_nil DmsfLink.deleted.where(:id => @folder_link.id).first
assert DmsfLink.visible.where(:id => @folder_link.id).first
end
def test_destroy
# File link
@file_link.delete true
assert_nil DmsfLink.find_by_id @file_link.id
# Folder link
@folder_link.delete true
assert_nil DmsfLink.find_by_id @folder_link.id
end
end

View File

@ -1,6 +1,7 @@
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2012 Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-14 Karel Picman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -18,7 +19,7 @@
require File.expand_path('../../test_helper.rb', __FILE__)
class DmsfFileTest < RedmineDmsf::Test::UnitTest
class DmsfLockTest < RedmineDmsf::Test::UnitTest
attr_reader :lock
fixtures :projects, :users, :dmsf_folders, :dmsf_files, :dmsf_file_revisions,
:roles, :members, :member_roles, :enabled_modules, :enumerations,

View File

@ -0,0 +1,16 @@
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
require 'test/unit'
require 'dmsf_file'
class TestDmsfFile < Test::Unit::TestCase
def test_foo
#TODO: Write test
flunk "TODO: Write test"
# assert_equal("foo", bar)
end
end