Commit 02ee1050 authored by Ruslan Zakirov's avatar Ruslan Zakirov
Browse files

make sure we can not create templates with duplicate names

parent bdec3fdb
......@@ -256,6 +256,16 @@ sub Create {
$args{'Queue'} = $QueueObj->Id;
}
return ( undef, $self->loc('Name is required') )
unless $args{Name};
{
my $tmp = $self->new( RT->SystemUser );
$tmp->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} );
return ( undef, $self->loc('Template with that name already exist') )
if $tmp->id;
}
my $result = $self->SUPER::Create(
Content => $args{'Content'},
Queue => $args{'Queue'},
......
......@@ -4,22 +4,26 @@ use strict;
use RT::Test tests => 12;
use_ok('RT::Template');
my $queue = RT::Test->load_or_create_queue( Name => 'Templates' );
ok $queue && $queue->id, "loaded or created a queue";
use_ok('RT::Template');
{
my $template = RT::Template->new(RT->SystemUser);
isa_ok($template, 'RT::Template');
}
{
my $template = RT::Template->new( RT->SystemUser );
isa_ok($template, 'RT::Template');
my ($val,$msg) = $template->Create(
Queue => $queue->id,
Name => 'InsertTest',
Name => 'Test',
Content => 'This is template content'
);
ok $val, "created a template" or diag "error: $msg";
ok my $id = $template->id, "id is defined";
is $template->Name, 'InsertTest';
is $template->Name, 'Test';
is $template->Content, 'This is template content', "We created the object right";
($val, $msg) = $template->SetContent( 'This is new template content');
......@@ -34,6 +38,17 @@ use_ok('RT::Template');
ok !$template->id, "can not load template after deletion";
}
note "can not create template with duplicate name";
{
clean_templates( Queue => $queue->id );
my $template = RT::Template->new( RT->SystemUser );
my ($val,$msg) = $template->Create( Queue => $queue->id, Name => 'Test' );
ok($val,$msg);
($val,$msg) = $template->Create( Queue => $queue->id, Name => 'Test' );
ok(!$val,$msg);
}
{
my $t = RT::Template->new(RT->SystemUser);
$t->Create(Name => "Foo", Queue => $queue->id);
......@@ -41,3 +56,17 @@ use_ok('RT::Template');
$t2->Load($t->Id);
ok($t2->QueueObj->id, "Got the template's queue objet");
}
sub clean_templates {
my %args = (@_);
my $templates = RT::Templates->new( RT->SystemUser );
$templates->Limit( FIELD => 'Queue', VALUE => $args{'Queue'} )
if defined $args{'Queue'};
$templates->Limit( FIELD => 'Name', VALUE => $_ )
foreach ref $args{'Name'}? @{$args{'Name'}} : ($args{'Name'}||());
while ( my $t = $templates->Next ) {
$t->Delete;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment