View | Details | Raw Unified | Return to bug 36654
Collapse All | Expand All

(-)a/Koha/Template/Plugin/Koha.pm (+21 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use base qw( Template::Plugin );
22
use base qw( Template::Plugin );
23
23
24
use Try::Tiny;
25
24
use C4::Context;
26
use C4::Context;
25
use Koha::Token;
27
use Koha::Token;
26
use Koha;
28
use Koha;
Lines 120-123 sub GenerateCSRF { Link Here
120
    return $csrf_token;
122
    return $csrf_token;
121
}
123
}
122
124
125
=head3 GetObjectById
126
127
Returns an singular object of the specified object set for the given id.
128
129
[% SET object = Koha.GetObjectById('Koha::Patrons', patron_id ) %]
130
131
=cut
132
133
sub GetObjectById {
134
    my ($self, $class, $id) = @_;
135
136
    my $object;
137
    try {
138
        $object = $class->find($id);
139
    };
140
141
    return $object;
142
}
143
123
1;
144
1;
(-)a/t/db_dependent/Koha/Template/Plugin/Koha.t (-2 / +31 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use Test::More tests => 3;
21
21
22
use Template::Context;
22
use Template::Context;
23
use Template::Stash;
23
use Template::Stash;
Lines 25-32 use Template::Stash; Link Here
25
use C4::Auth;
25
use C4::Auth;
26
use Koha::Cache::Memory::Lite;
26
use Koha::Cache::Memory::Lite;
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::Patrons;
28
use Koha::Template::Plugin::Koha;
29
use Koha::Template::Plugin::Koha;
29
30
31
use t::lib::TestBuilder;
32
30
my $schema = Koha::Database->new->schema;
33
my $schema = Koha::Database->new->schema;
31
34
32
subtest 'GenerateCSRF() tests' => sub {
35
subtest 'GenerateCSRF() tests' => sub {
Lines 75-77 subtest 'GenerateCSRF - New CSRF token generated everytime we need one' => sub { Link Here
75
    $schema->storage->txn_rollback;
78
    $schema->storage->txn_rollback;
76
79
77
};
80
};
78
- 
81
82
subtest 'GetObjectById tests' => sub {
83
    plan tests => 3;
84
85
    $schema->storage->txn_begin;
86
87
    my $session = C4::Auth::get_session('');
88
89
    my $stash   = Template::Stash->new( { sessionID => $session->id } );
90
    my $context = Template::Context->new( { STASH => $stash } );
91
92
    my $builder = t::lib::TestBuilder->new;
93
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
94
95
    my $plugin = Koha::Template::Plugin::Koha->new($context);
96
97
    my $patron2 = $plugin->GetObjectById('Koha::Patrons', $patron->id );
98
    is( $patron->id, $patron2->id, "Got correct object via GetObjectById" );
99
100
    my $invalid = $plugin->GetObjectById('Koha::Patrons', 'INVALID_ID' );
101
    is( $invalid, undef, "Invalid ID returns undef" );
102
103
    $invalid = $plugin->GetObjectById('Koha::Nothing', 1 );
104
    is( $invalid, undef, "Invalid objects name returns undef" );
105
106
    $schema->storage->txn_rollback;
107
};

Return to bug 36654