Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-85400

PHPunit: Hard to test code that uses encryption

XMLWordPrintable

      When test code uses \core\encryption, this will create a key file which, for security and reliability reasons, is set to 0400 (u=r) permission.

      (In a real system, it would be bad if the key file gets written because then all your encrypted admin settings would irrevocably stop working.)

      However, this is annoying when writing PHPunit tests (for unrelated code that happens to use encryption) because the reset after the test completes is unable to delete the data directory. To make this work, you have to add code like this in each test case that uses encryption:

          #[\Override]
          protected function tearDown(): void {
              // A key file may be generated, change permissions so it can be deleted.
              $keyfile = \core\encryption::get_key_file(\core\encryption::METHOD_SODIUM);
              if (file_exists($keyfile)) {
                  chmod($keyfile, 0700);
              }
              parent::tearDown();
          }
      

      This is pretty rubbish.

      There is already code to prevent it breaking Behat tests by disabling the file permissions in Behat runs. I think we should do the same thing for unit tests.

      https://github.com/moodle/moodle/blob/main/lib/classes/encryption.php#L64

            quen Sam Marshall
            quen Sam Marshall
            Tim Hunt Tim Hunt
            Simey Lameze Simey Lameze
            CiBoT CiBoT
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 15 minutes
                15m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.