From bba600676fbca13a7258b34716c519da94f8b800 Mon Sep 17 00:00:00 2001
From: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Date: Mon, 3 Mar 2025 16:53:46 +0000
Subject: [PATCH] Bug 37901: Add Tests

Example reports that can be written for ILL pseudonymized data:

--Total of requests of a given request type (article)--

SELECT
  COUNT(*) AS count
FROM
  pseudonymized_transactions pt
WHERE
  EXISTS (
    SELECT 1
    FROM pseudonymized_metadata_values pmv
    WHERE pmv.transaction_id = pt.id
    AND pmv.key = 'type'
    AND pmv.value = 'article'
  );

--Add backend info to listing--

SELECT
  pt.*,
  (SELECT pmv.value FROM pseudonymized_metadata_values pmv WHERE pmv.transaction_id = pt.id AND pmv.key = 'type') AS type,
  (SELECT pmv.value FROM pseudonymized_metadata_values pmv WHERE pmv.transaction_id = pt.id AND pmv.key = 'backend') AS backend
FROM
  pseudonymized_transactions pt;

--List all pseudonymized transactions and respective metadata values--

SELECT
  pt.*,
  table1.metadata AS borrower_attributes,
  table2.metadata AS illrequestattributes
FROM
  pseudonymized_transactions pt
  LEFT JOIN (
    SELECT
      transaction_id,
      GROUP_CONCAT(DISTINCT CONCAT(`key`, ':', value)) AS metadata
    FROM
      pseudonymized_metadata_values
    WHERE tablename = 'borrower_attributes'
    GROUP BY
      transaction_id
  ) table1 ON pt.id = table1.transaction_id
  LEFT JOIN (
    SELECT
      transaction_id,
      GROUP_CONCAT(DISTINCT CONCAT(`key`, ':', value)) AS metadata
    FROM
      pseudonymized_metadata_values
    WHERE tablename = 'illrequestattributes'
    GROUP BY
      transaction_id
  ) table2 ON pt.id = table2.transaction_id;

---

Test plan:
1) Add a <bcrypt_settings> under <config> in /etc/koha/sites/kohadev/koha.conf.xml:
  $ <bcrypt_settings>$2a$08$9lmorEKnwQloheaCLFIfje</bcrypt_settings> <!-- taken from Pseudonymization.t -->
2) Restart plack & worker:
$ koha-plack --restart kohadev && sudo koha-worker --restart kohadev
3) Enable ILLModule and Pseudonymization system preferences
4) Check 'Keep for pseudonymization' at the only existing patron attribute type:
http://localhost:8081/cgi-bin/koha/admin/patron-attr-types.pl?op=edit_attribute_type&code=SHOW_BCODE
5) Save. Edit that attribute type for 'koha' patron:
http://localhost:8081/cgi-bin/koha/members/memberentry.pl?op=edit_form&destination=circ&borrowernumber=51
6) Scroll down, set a value for 'Show barcode on the summary screen items listings:'. Save.
7) Do a checkout for that 'koha' patron.
8) Confirm the pseudonymized_transaction has been added alongside its metadata (SHOW_BCODE), run report #3 from the examples listed above.
This concludes the 'preparation' side of testing, ensuring that borrower_attributes are kept for pseudonymization
9) Create a new ILL request of type 'book'.
10) Run the report from 8) again. Verify the ILL pseudonymized transaction and its related pseudonymized metadata is there.

Run tests:

prove t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t
prove t/db_dependent/Koha/PseudonymizedTransaction.t
prove t/db_dependent/Koha/Pseudonymization.t
---
 t/db_dependent/Koha/Pseudonymization.t | 34 +++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t
index 6f16895c285..fc6099cbb60 100755
--- a/t/db_dependent/Koha/Pseudonymization.t
+++ b/t/db_dependent/Koha/Pseudonymization.t
@@ -140,7 +140,7 @@ subtest 'Koha::Anonymized::Transactions tests' => sub {
 
 subtest 'PseudonymizedMetadataValues tests' => sub {
 
-    plan tests => 5;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
@@ -232,5 +232,37 @@ subtest 'PseudonymizedMetadataValues tests' => sub {
     is( $attribute_2->value, $attribute_values->[2]->{attribute} );
     is( $attribute_2->key,   $attribute_values->[2]->{code} );
 
+    my $ill_request = $builder->build_sample_ill_request();
+    $builder->build(
+        {
+            source => 'Illrequestattribute',
+            value  => { illrequest_id => $ill_request->illrequest_id, type => 'type', value => 'book' }
+        }
+    );
+
+    my $statistic2 = Koha::Statistic->new(
+        {
+            type           => 'ill_request',
+            branch         => $library->branchcode,
+            itemnumber     => undef,
+            borrowernumber => $patron->borrowernumber,
+            itemtype       => undef,
+            location       => undef,
+            illrequest_id  => $ill_request->illrequest_id,
+            ccode          => undef,
+        }
+    );
+
+    my $p2 = Koha::PseudonymizedTransaction->new_from_statistic($statistic2)->store;
+
+    my $ill_metadata_values =
+        Koha::Database->new->schema->resultset('PseudonymizedMetadataValue')
+        ->search( { transaction_id => $p2->id, tablename => 'illrequestattributes' }, { order_by => 'value' } );
+
+    my $ill_metadata_value = $ill_metadata_values->next;
+
+    is( $ill_metadata_value->key,   'type' );
+    is( $ill_metadata_value->value, 'book' );
+
     $schema->storage->txn_rollback;
 };
-- 
2.39.5