Problem/Motivation

The recipes imported during the installation of the demo_umami profile are held in a csv file, with one recipe per line.
This makes making and reviewing changes to the recipes very difficult, as git diff will just show you that the entire line has changed.

Proposed resolution

Move the recipe instructions to their own text file. Reference the name of the file in the csv where we currently have the entire instruction text.

Remaining tasks

All the things.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

Eli-T created an issue. See original summary.

eli-t’s picture

Title: Move recipe text out of csv file in to their own text file. » Move recipe instruction out of csv file in to their own text file.
Issue summary: View changes

Improved language to reflect correct field name is Recipe Instruction rather than Body.

eli-t’s picture

Status: Active » Needs review
StatusFileSize
new21.11 KB

First attempt. I've actually put the instructions in .html files, as they are html, code editors will highlight/reformat correctly and it's not an issue if they are browsed directly.

eli-t’s picture

StatusFileSize
new22.22 KB

Suggestion from @mark_conroy and @smaz to add a .htaccess file to stop the html files being served

markconroy’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me and makes the CSV with things like alt tags for images waaaaay easier to read/edit. Marking as RTBC (this was also discussed on the weekly call today).

eli-t’s picture

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests
+++ b/core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php
@@ -219,7 +219,12 @@ protected function importRecipes() {
+          $recipe_instruction_handle = fopen($recipe_instruction_path, 'r');
...
+            $recipe_instructions = fread($recipe_instruction_handle, filesize($recipe_instruction_path));

any reason not to use file_get_contents instead here, no need to use fread/fsize/fopen dance for such small files

We need a test for the .htaccess protection.

See #2941488: Add a .htaccess file to core/profiles/demo_umami/modules/demo_umami_content/default_content/images to prevent direct downloading of images for sample test.

+++ b/core/profiles/demo_umami/modules/demo_umami_content/default_content/recipe_instructions/.htaccess
@@ -0,0 +1,23 @@
+# Set the catch-all handler to prevent scripts from being executed.
+SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
+<Files *>
+  # Override the handler again if we're run later in the evaluation list.
+  SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003
+</Files>
+
+# If we know how to do it safely, disable the PHP engine entirely.
+<IfModule mod_php5.c>
+  php_flag engine off
+</IfModule>

I don't think we need this, there are no php files in this folder right?

eli-t’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new22.53 KB

Patch to address everything in #7

  • Adds a test of the htaccess
  • Trims the htaccess as recommended as no PHP present
  • Replaces fopen/fread with file_get_contents as recommended

In addition, the html files containing the recipes have had some newlines removed - these would be ignored when viewing them as html, but our import or filtering process was converting them in to html when displayed.

markconroy’s picture

Status: Needs review » Reviewed & tested by the community

I can't access images by going directly to their file.
I can't access html files by going directly to their file.
.htaccess for php is removed

Let's RTBC this.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/profiles/demo_umami/modules/demo_umami_content/default_content/recipe_instructions/chocolate-brownie-umami.html
@@ -0,0 +1,9 @@
+<ol>
+  <li>Preheat the oven to 180C and grease a 8 inch cake tin.</li>
+  <li>Sift the flour and cocoa powder into a bowl and add the sugar and salt.</li>
+  <li>Add the oil, a little at time, mixing well to avoid lumps. Add the soy milk and vanilla extract and mix thoroughly.
+  </li>
+  <li>Gently pour into the cake tin and spread out until even. Bake for about 15 minutes and check with a knife (the knife should come out clean when inserted into the brownies 2cm from the edge).
+  </li>
+  <li>Allow to cool in the tin for a few minutes before cooling on a wire rack.</li>
+</ol>

What is the reason for some closing list items on its own line? Should they all be oneliners instead?

eli-t’s picture

What is the reason for some closing list items on its own line? Should they all be oneliners instead?

Before this patch, the html for each recipe was contained in a single line on a CSV. To make it more readable I auto-formatted it in the HTML files using PHPStorm's Reformat Code option. The only manual change made was to remove the newlines that introduced, as mentioned in #8. I suspect where PHPStorm had introduced newlines, it moves the closing LF element to the following line.

Is there a standard we need to review .html files against?

kjay’s picture

Here's a patch that updates @eli-t's patch in #8 with fixes for @Gábor Hojtsy's feedback turning all the list items's into one liners.

kjay’s picture

StatusFileSize
new10.57 KB

Sorry, interdiff for the previous comment.

markconroy’s picture

Status: Needs review » Reviewed & tested by the community

It's a thing of beauty! Thanks @gabor for the suggestion.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Whilst reviewing this, I checked if we had test coverage for body fields already, which would verify that this feature worked.

But unfortunately, we don't.

We already have \Drupal\Tests\demo_umami_content\Functional\UninstallDefaultContentTest::assertRecipesImported

In which we do this

$this->assertCount(1, $nodes);

Can we add the following:

$node = reset($nodes);
$this->assertContains('Mix the some of the milk and water in a jug', $node->field_recipe_instructions->processed);

Then we're sure this doesn't get broken in the future.

eli-t’s picture

Status: Needs work » Needs review
StatusFileSize
new23.35 KB
new669 bytes

Suggestion in #15 implemented.

Status: Needs review » Needs work

The last submitted patch, 16: 2940362_16_add_a_test.patch, failed testing. View results

eli-t’s picture

Status: Needs work » Needs review
StatusFileSize
new15.1 KB
new6.9 KB

Field name was incorrect in the new test.

Status: Needs review » Needs work

The last submitted patch, 18: 2940362_18_add_a_test.patch, failed testing. View results

eli-t’s picture

Status: Needs work » Needs review
StatusFileSize
new23.35 KB
new860 bytes

#18 is nonsense, it was missing the recipes.

This should be good. Apologies for the noise.

Status: Needs review » Needs work

The last submitted patch, 20: 2940362_20_actually_include_recipes.patch, failed testing. View results

eli-t’s picture

Status: Needs work » Needs review
StatusFileSize
new23.34 KB
new855 bytes

Previous test failed because $node->processed was unavailable. I've changed this to $node->value and run the tests locally and it passes. As we are only testing that the content is loaded, and not how it processed, I think this is ok.

navneet0693’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new800.38 KB
new48.13 KB

This should be good.

Instructions are in proper format. Here's an example:

receipe instructions screenshot

Direct access to file results in forbidden response:

forbidden response screenshot

gábor hojtsy’s picture

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests

Thanks for taking care of all the bits raised, committed!

+++ b/core/profiles/demo_umami/modules/demo_umami_content/tests/src/Functional/UninstallDefaultContentTest.php
@@ -90,7 +90,7 @@
+    $this->assertContains('Mix the some of the milk and water in a jug', $node->field_recipe_instruction->value);

I would like to point out that the chosen sample text has some language issues: "the some of the" does not sound correct :) That said this is a pre-existing problem and can in fact be fixed easier once the files are on their own.

  • Gábor Hojtsy committed 39e2179 on 8.6.x
    Issue #2940362 by Eli-T, kjay, navneet0693, markconroy, larowlan, Gábor...
eli-t’s picture

Thanks Gábor! WRT the language in #25, that's been raised as a comment on https://www.drupal.org/project/drupal/issues/2940146#comment-12465501

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.