From 0332afd17766aefa678c6b67c424338a0642a986 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Tue, 23 Jul 2024 15:52:30 -0300
Subject: [PATCH] Bug 36736: Tests [POC]

---
 t/db_dependent/Koha/Plugins/Plugins.t         | 17 ++++-
 t/lib/plugins/Koha/Plugin/Test.pm             | 18 +++++
 .../Koha/Plugin/Test/TestBackgroundJob.pm     | 56 ++++++++++++++++
 .../Koha/Plugin/Test/lib/Koha/Potato.pm       | 40 ++++++++++++
 .../Koha/Plugin/Test/lib/Koha/Potatoes.pm     | 45 +++++++++++++
 .../Test/lib/Koha/Schema/Result/Potatoe.pm    | 65 +++++++++++++++++++
 6 files changed, 240 insertions(+), 1 deletion(-)
 create mode 100644 t/lib/plugins/Koha/Plugin/Test/TestBackgroundJob.pm
 create mode 100644 t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potato.pm
 create mode 100644 t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potatoes.pm
 create mode 100644 t/lib/plugins/Koha/Plugin/Test/lib/Koha/Schema/Result/Potatoe.pm

diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t
index 1c8f96ab156..a745e83720d 100755
--- a/t/db_dependent/Koha/Plugins/Plugins.t
+++ b/t/db_dependent/Koha/Plugins/Plugins.t
@@ -64,7 +64,10 @@ subtest 'call() tests' => sub {
 
     my @plugins;
 
-    warning_is { @plugins = $plugins->InstallPlugins; } undef;
+    # Koha::Plugin::Test generates 3 redefined sub warnings, no other warnings
+    # are expected.
+    warnings_like { @plugins = $plugins->InstallPlugins; }
+    [ qr/Subroutine .* redefined/, qr/Subroutine .* redefined/, qr/Subroutine .* redefined/ ];
 
     foreach my $plugin (@plugins) {
         $plugin->enable();
@@ -315,6 +318,18 @@ subtest 'Koha::Plugin::Test' => sub {
     @names = map { $_->get_metadata()->{'name'} } @plugins;
     is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
 
+    subtest 'background jobs' => sub {
+
+        plan tests => 1;
+
+        my $job = Koha::BackgroundJob->new(
+            { type => 'test_background_job' }
+        );
+        $job->process;
+
+        is(1,1);
+    };
+
     $schema->storage->txn_rollback;
 };
 
diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm
index 255fc90a122..15e24b8662a 100644
--- a/t/lib/plugins/Koha/Plugin/Test.pm
+++ b/t/lib/plugins/Koha/Plugin/Test.pm
@@ -5,8 +5,10 @@ use Modern::Perl;
 
 use Koha::Exception;
 use Koha::Plugins::Tab;
+use Koha::Schema;
 
 use MARC::Field;
+use Module::Metadata;
 use Mojo::JSON qw( decode_json );
 
 use t::lib::TestBuilder;
@@ -28,6 +30,22 @@ our $metadata = {
     my_example_tag  => 'find_me',
 };
 
+BEGIN {
+    my $path = Module::Metadata->find_module_by_name(__PACKAGE__);
+    $path =~ s!\.pm$!/lib!;
+    unshift @INC, $path;
+
+    require Koha::Potatoes;
+    require Koha::Schema::Result::Potatoe;
+
+    # register the additional schema classes
+    Koha::Schema->register_class( Potatoe => 'Koha::Schema::Result::Potatoe' );
+
+    # ... and force a refresh of the database handle so that it includes
+    # the new classes
+    Koha::Database->schema( { new => 1 } );
+}
+
 ## This is the minimum code required for a plugin's 'new' method
 ## More can be added, but none should be removed
 sub new {
diff --git a/t/lib/plugins/Koha/Plugin/Test/TestBackgroundJob.pm b/t/lib/plugins/Koha/Plugin/Test/TestBackgroundJob.pm
new file mode 100644
index 00000000000..79fcdc15fe1
--- /dev/null
+++ b/t/lib/plugins/Koha/Plugin/Test/TestBackgroundJob.pm
@@ -0,0 +1,56 @@
+package Koha::BackgroundJob::TestBackgroundJob;
+
+# This file is part of Koha.
+#
+# Koha 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 3 of the License, or
+# (at your option) any later version.
+#
+# Koha 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 Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use base 'Koha::BackgroundJob';
+
+=head1 NAME
+
+TestBackgroundJob - Dummy background job for testing
+
+This is a subclass of Koha::BackgroundJob.
+
+=head1 API
+
+=head2 Class methods
+
+=head3 job_type
+
+Define the job type of this job.
+
+=cut
+
+sub job_type {
+    return 'test_background_job';
+}
+
+=head3 process
+
+=cut
+
+sub process {
+    my ( $self, $args ) = @_;
+
+    use DDP;
+    my $schema = Koha::Database->new()->schema();
+    p($schema->source_registrations->{Potatoe});
+}
+
+1;
diff --git a/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potato.pm b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potato.pm
new file mode 100644
index 00000000000..afd69f0c5e8
--- /dev/null
+++ b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potato.pm
@@ -0,0 +1,40 @@
+package Koha::Potato;
+
+# This file is part of Koha.
+#
+# Koha 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 3 of the License, or
+# (at your option) any later version.
+#
+# Koha 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 Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Potato - Koha Potato Object class
+
+=head1 API
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Potatoe';
+}
+
+1;
diff --git a/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potatoes.pm b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potatoes.pm
new file mode 100644
index 00000000000..1e775e0d60c
--- /dev/null
+++ b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Potatoes.pm
@@ -0,0 +1,45 @@
+package Koha::Potatoes;
+
+# This file is part of Koha.
+#
+# Koha 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 3 of the License, or
+# (at your option) any later version.
+#
+# Koha 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 Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Koha::Database;
+use Koha::Potato;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Potatoes - Koha Potato Object set class
+
+=head1 API
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Potatoe';
+}
+
+sub object_class {
+    return 'Koha::Potato';
+}
+
+1;
diff --git a/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Schema/Result/Potatoe.pm b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Schema/Result/Potatoe.pm
new file mode 100644
index 00000000000..148538524da
--- /dev/null
+++ b/t/lib/plugins/Koha/Plugin/Test/lib/Koha/Schema/Result/Potatoe.pm
@@ -0,0 +1,65 @@
+use utf8;
+package Koha::Schema::Result::Potatoe;
+
+# Created by DBIx::Class::Schema::Loader
+# DO NOT MODIFY THE FIRST PART OF THIS FILE
+
+=head1 NAME
+
+Koha::Schema::Result::Potatoe
+
+=cut
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+=head1 TABLE: C<potatoes>
+
+=cut
+
+__PACKAGE__->table("potatoes");
+
+=head1 ACCESSORS
+
+=head2 potato_id
+
+  data_type: 'integer'
+  is_auto_increment: 1
+  is_nullable: 0
+
+=head2 name
+
+  data_type: 'varchar'
+  is_nullable: 0
+  size: 255
+
+=cut
+
+__PACKAGE__->add_columns(
+  "potato_id",
+  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
+  "name",
+  { data_type => "varchar", is_nullable => 0, size => 255 },
+);
+
+=head1 PRIMARY KEY
+
+=over 4
+
+=item * L</potato_id>
+
+=back
+
+=cut
+
+__PACKAGE__->set_primary_key("potato_id");
+
+
+# Created by DBIx::Class::Schema::Loader v0.07051 @ 2024-07-23 13:28:28
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:PpqJllhQ+/CfOjwd/smUjA
+
+
+# You can replace this text with custom code or comments, and it will be preserved on regeneration
+1;
-- 
2.45.2