Skip to content

Commit

Permalink
Add the ability to add images to an existing album via the stream pag…
Browse files Browse the repository at this point in the history
…e form.
  • Loading branch information
nkoporec committed Jul 5, 2023
1 parent 057260b commit 10acc5b
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ protected function getOptions(FieldableEntityInterface $entity) {
];
$default_value = $entity->get('field_album')->target_id;
if ($entity->isNew() || !isset($options[$default_value])) {
// To attach to the existing Album, "Post" should be added using
// button on the Album page.
return $empty_options;
$new_options = [
'_none' => $empty_options['_none'],
];

// Existing albums.
foreach ($options as $key => $value) {
$new_options[$key] = $value;
}

$new_options['_add'] = $empty_options['_add'];

return $new_options;
}
// Return only default (previously saved album) and helper options.
return $empty_options + [$default_value => $options[$default_value]];
Expand Down Expand Up @@ -127,6 +136,14 @@ public static function validateElement(array $element, FormStateInterface $form_
$element['#value'] = '_none';
}
}
elseif ($element['#value'] !== '_none' && $has_images) {
/** @var \Drupal\node\NodeInterface|null $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($element['#value']);
if ($node) {
$element['#value'] = $node->id();
$form_state->set('album', TRUE);
}
}
elseif ($element['#value'] !== '_none' && !$has_images) {
$element['#value'] = '_none';
}
Expand Down
1 change: 1 addition & 0 deletions tests/behat/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ default:
- Drupal\social\Behat\DatabaseContext:
- '%paths.base%/fixture'
- Drupal\DrupalExtension\Context\BatchContext
- Drupal\social\Behat\AlbumContext
- Drupal\social\Behat\BookContext
- Drupal\social\Behat\CKEditorContext
- Drupal\social\Behat\ConfigContext
Expand Down
114 changes: 114 additions & 0 deletions tests/behat/features/bootstrap/AlbumContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Drupal\social\Behat;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\MinkExtension\Context\RawMinkContext;
use Drupal\DrupalExtension\Context\MinkContext;

/**
* Defines test steps around the usage of albums.
*/
class AlbumContext extends RawMinkContext {

use NodeTrait;
use GroupTrait;

private const CREATE_PAGE = "/node/add/album";

/**
* Keep track of what we created, so we can use them for validating.
*/
private array $created = [];

/**
* The Drupal mink context is useful for validation of content.
*/
private MinkContext $minkContext;

/**
* Make some contexts available here so we can delegate steps.
*
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();

$this->minkContext = $environment->getContext(SocialMinkContext::class);
}

/**
* Check that an album that was just created is properly shown.
*
* @Then /^(?:|I )should see the album I just created$/
*/
public function thenIShouldSeeTheAlbumIJustCreated() : void {
$fields = end($this->created);

$this->minkContext->assertPageContainsText("Album {$fields['title']} is successfully created. Now you can add images to this album.");
}

/**
* Fill out the album creation form and submit.
*
* Example: When I create an album using its creation page:
* | Title | My Album |
* | visibility | public |
* | group | My group |
*
* @When /^(?:|I )create an album using its creation page:$/
*/
public function whenICreateAnAlbumUsingTheForm(TableNode $fields): void {
// Go to the form.
$this->visitPath(self::CREATE_PAGE);
$page = $this->getSession()->getPage();

// Fill in the fields.
$album = [];
foreach ($fields->getRowsHash() as $field => $value) {
$key = strtolower($field);
$album[$key] = $value;
// We must be more specific for the title field since there could be more
// than one on the page (e.g. the menu title).
if ($key === "title") {
$fieldset = $page
->find("named", ["fieldset", "Basic information"]);

if ($fieldset === NULL) {
throw new ElementNotFoundException($this->getSession()->getDriver(), "fieldset", "named", "Basic Information");
}

$fieldset->fillField($field, $value);
}
elseif ($key === "group") {
$group_id = $this->getNewestGroupIdFromTitle($value);
if ($group_id === NULL) {
throw new \Exception("Group '${$value}' does not exist.");
}
$page->selectFieldOption($field, $group_id);
// Changing the group of an album updates the visibility settings so we
// must wait for that to be complete.
$this->minkContext->iWaitForAjaxToFinish();
}
elseif ($key === "visibility") {
$page->selectFieldOption("field_content_visibility", $value);
}
else {
$page->fillField($field, $value);
}

// Changing the group of an album updates the visibility settings so we must
// wait for that to be complete.
if ($key === "group") {
$this->minkContext->iWaitForAjaxToFinish();
}
}

// Submit the page.
$page->pressButton("Create album");

$this->created[] = $album;
}
}
21 changes: 21 additions & 0 deletions tests/behat/features/capabilities/post/post-album-existing.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@api @post @stability @PROD-25398 @database @stability-3 @album @post-album
Feature: Create Post with Photo and add it to an existing Album
Benefit: In order to share knowledge with people
Role: As a Verified
Goal/desire: I want to create Posts with photo and add it to the existing album.

Scenario: Successfully add an image to the album via the stream post form

Given I enable the module "social_album"
Given users:
| name | status | pass | roles |
| PostUser | 1 | PostUser | verified |
And I am logged in as "PostUser"

When I create an album using its creation page:
| Title | This is my first album. |
Then I should see the album I just created

Given I am on "/stream"
Then I should see "Add images"
And I should see "This is my first album." in the ".field--name-field-album" element

0 comments on commit 10acc5b

Please sign in to comment.