Commit 7d46391c authored by Fran Garcia-Linares's avatar Fran Garcia-Linares Committed by Tim Plunkett
Browse files

Issue #3301989 by fjgarlin, tim.plunkett: Fix regression in RandomDataPlugin

parent 26e6e986
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -142,7 +142,8 @@ class RandomDataPlugin extends ProjectBrowserSourceBase implements ContainerFact

    if (!empty($query['machine_name'])) {
      $projects = array_values(array_filter($projects, function ($project) use ($query) {
        return $project->field_project_machine_name === $query['machine_name'];
        /** @var \Drupal\project_browser\ProjectBrowser\Project $project */
        return $project->getMachineName() === $query['machine_name'];
      }));
    }

+109 −45
Original line number Diff line number Diff line
@@ -7,14 +7,8 @@ use Drupal\Component\Utility\Unicode;

/**
 * Defines a single Project.
 *
 * Properties set by
 * \Drupal\project_browser\ProjectBrowser\ProjectBrowserSourceInterface
 * are used by front-end.
 *
 * These properties will be exposed to the frontend.
 */
class Project implements ProjectInterface {
class Project implements \JsonSerializable {

  /**
   * Whether the project is compatible with the current version of Drupal.
@@ -183,13 +177,13 @@ class Project implements ProjectInterface {
    }

    if (is_array($project)) {
      $this->setAuthor($project['author']);
      $this->setAuthor((array) $project['author']);
      $this->setCreatedTimestamp($project['created']);
      $this->setChangedTimestamp($project['changed']);
      $this->setProjectStatus($project['status']);
      $this->setProjectTitle($project['title']);
      $this->setId($project['nid']);
      $this->setSummary($project['body']);
      $this->setSummary((array) $project['body']);
      $this->setImages($project['field_project_images']);
      $this->setMaintenanceStatus($project['field_maintenance_status']);
      $this->setMachineName($project['field_project_machine_name']);
@@ -209,170 +203,240 @@ class Project implements ProjectInterface {
  }

  /**
   * {@inheritdoc}
   * Set the author of Project.
   *
   * @param array $author
   *   Author in array format.
   */
  public function setAuthor($author) {
  public function setAuthor(array $author) {
    $this->author = $author;
  }

  /**
   * {@inheritdoc}
   * Set the project created timestamp.
   *
   * @param int $created
   *   Timestamp.
   */
  public function setCreatedTimestamp(int $created) {
    $this->created = $created;
  }

  /**
   * {@inheritdoc}
   * Set the project changed timestamp.
   *
   * @param int $changed
   *   Timestamp.
   */
  public function setChangedTimestamp(int $changed) {
    $this->changed = $changed;
  }

  /**
   * {@inheritdoc}
   * Set the project status.
   *
   * @param int $status
   *   Status of the project.
   */
  public function setProjectStatus(int $status) {
    $this->status = $status;
  }

  /**
   * {@inheritdoc}
   * Set the project title.
   *
   * @param string $title
   *   Title of the project.
   */
  public function setProjectTitle(string $title) {
    $this->title = $title;
  }

  /**
   * {@inheritdoc}
   * Set the unique identifier of Project, eg nid.
   *
   * @param string $id
   *   ID of the project.
   */
  public function setId($id) {
  public function setId(string $id) {
    $this->id = $id;
  }

  /**
   * {@inheritdoc}
   * Set the project short description.
   *
   * @param array $body
   *   Body in array format.
   */
  public function setSummary($body) {
    if (is_object($body)) {
      $body = (array) $body;
    }

  public function setSummary(array $body) {
    $this->body = $body;
    if (is_array($body)) {
    if (empty($this->body['summary'])) {
      $this->body['summary'] = $this->body['value'] ?? '';
    }
    $this->body['summary'] = Html::escape(strip_tags($this->body['summary']));
    $this->body['summary'] = Unicode::truncate($this->body['summary'], 200, TRUE, TRUE);
  }
  }

  /**
   * {@inheritdoc}
   * Set the images associated with the project.
   *
   * @param array $images
   *   Images in array format.
   */
  public function setImages(array $images) {
    $this->images = $images;
  }

  /**
   * {@inheritdoc}
   * Set the project maintenance status code.
   *
   * @param array $maintenance_status
   *   Maintenance status.
   */
  public function setMaintenanceStatus($maintenance_status) {
  public function setMaintenanceStatus(array $maintenance_status) {
    $this->maintenanceStatus = $maintenance_status;
  }

  /**
   * {@inheritdoc}
   * Set the project machine name.
   *
   * @param string $machine_name
   *   Machine name of the module.
   */
  public function setMachineName(string $machine_name) {
    $this->machineName = $machine_name;
  }

  /**
   * {@inheritdoc}
   * Set the composer namespace.
   *
   * @param string $composer_namespace
   *   Composer namespace of the module.
   */
  public function setComposerNamespace(string $composer_namespace) {
    $this->composerNamespace = $composer_namespace;
  }

  /**
   * {@inheritdoc}
   * Set the categories this project belongs.
   *
   * @param array $categories
   *   Module category ids.
   */
  public function setModuleCategories($category) {
    $this->categories = $category;
  public function setModuleCategories(array $categories) {
    $this->categories = $categories;
  }

  /**
   * {@inheritdoc}
   * Set the project security coverage status.
   *
   * @param string $coverage
   *   Coverage value.
   */
  public function setSecurityAdvisoryCoverage(string $coverage) {
    $this->coverage = $coverage;
  }

  /**
   * {@inheritdoc}
   * Set the URL to the project page, where someone could learn more about this project.
   *
   * @param string $machine_name
   *   Machine name of the module.
   */
  public function setProjectUrl(string $machine_name) {
    $this->url = 'https://www.drupal.org/project/' . $machine_name;
  }

  /**
   * {@inheritdoc}
   * Set the release-wise project usage count.
   *
   * @param array $usage
   *   Release-wise project usage count.
   */
  public function setProjectUsage($usage) {
    $this->usage = (array) $usage;
  public function setProjectUsage(array $usage) {
    $this->usage = $usage;
  }

  /**
   * {@inheritdoc}
   * Set the total usage count of all releases.
   *
   * @param int $usage_total
   *   Total usage.
   */
  public function setProjectUsageTotal(int $usage_total) {
    $this->projectUsageTotal = $usage_total;
  }

  /**
   * {@inheritdoc}
   * Set the project star user count.
   *
   * @param int $star_user_count
   *   Start user count value.
   */
  public function setProjectStarUserCount(int $star_user_count) {
    $this->starUserCount = $star_user_count;
  }

  /**
   * {@inheritdoc}
   * Set if the project is considered active or not.
   *
   * @param bool $is_active
   *   Value to set.
   */
  public function setIsActive(bool $is_active) {
    $this->isActive = $is_active;
  }

  /**
   * {@inheritdoc}
   * Set if the project is considered covered or not.
   *
   * @param bool $is_covered
   *   Value to set.
   */
  public function setIsCovered(bool $is_covered) {
    $this->isCovered = $is_covered;
  }

  /**
   * {@inheritdoc}
   * Set if the project is considered maintained or not.
   *
   * @param bool $is_maintained
   *   Value to set.
   */
  public function setIsMaintained(bool $is_maintained) {
    $this->isMaintained = $is_maintained;
  }

  /**
   * {@inheritdoc}
   * Set whether the project is compatible with the current Drupal installation.
   *
   * @param bool $compatible
   *   Whether the project is compatible or not.
   */
  public function setIsCompatible(bool $compatible) {
    $this->isCompatible = $compatible;
  }

  /**
   * {@inheritdoc}
   * Warnings related to installing a given module.
   *
   * @param string[] $warnings
   *   Warnings about the module to present the the user.
   */
  public function setWarnings(array $warnings) {
    $this->warnings = $warnings;
  }

  /**
   * Returns the machine name of the project.
   *
   * @return string
   *   The machine name of the project.
   */
  public function getMachineName(): string {
    return $this->machineName;
  }

  /**
   * {@inheritdoc}
   */
+0 −145
Original line number Diff line number Diff line
<?php

namespace Drupal\project_browser\ProjectBrowser;

/**
 * Defines an interface for API parameters.
 */
interface ProjectInterface extends \JsonSerializable {

  /**
   * Author of Project.
   */
  public function setAuthor($author);

  /**
   * Project created timestamp.
   */
  public function setCreatedTimestamp(int $created);

  /**
   * Project changed timestamp.
   */
  public function setChangedTimestamp(int $changed);

  /**
   * Project status.
   */
  public function setProjectStatus(int $status);

  /**
   * Project title.
   */
  public function setProjectTitle(string $title);

  /**
   * Unique identifier of Project, eg nid.
   */
  public function setId($id);

  /**
   * Project short description.
   */
  public function setSummary($body);

  /**
   * Project maintenance status code.
   */
  public function setMaintenanceStatus($maintenance_status);

  /**
   * Project machine name.
   */
  public function setMachineName(string $machine_name);

  /**
   * Composer namespace.
   */
  public function setComposerNamespace(string $composer_namespace);

  /**
   * Images associated with the project. Logo should be the first one.
   */
  public function setImages(array $images);

  /**
   * Categories this project belongs.
   *
   * This must use one of the Drupal categories, see
   * https://www.drupal.org/project/project_module
   * to see the list.
   *
   * @param object|array $category
   *   Module category ids.
   */
  public function setModuleCategories($category);

  /**
   * Project security coverage status.
   */
  public function setSecurityAdvisoryCoverage(string $coverage);

  /**
   * URL to the project page, where someone could learn more about this project.
   */
  public function setProjectUrl(string $machine_name);

  /**
   * Release-wise project usage count.
   *
   * @param object|array $usage
   *   Release-wise project usage count.
   */
  public function setProjectUsage($usage);

  /**
   * Total usage count of all releases.
   */
  public function setProjectUsageTotal(int $usage_total);

  /**
   * Project star user count.
   */
  public function setProjectStarUserCount(int $star_user_count);

  /**
   * Set if the project is considered active or not.
   *
   * @param bool $is_active
   *   Value to set.
   */
  public function setIsActive(bool $is_active);

  /**
   * Set if the project is considered covered or not.
   *
   * @param bool $is_covered
   *   Value to set.
   */
  public function setIsCovered(bool $is_covered);

  /**
   * Set if the project is considered maintained or not.
   *
   * @param bool $is_maintained
   *   Value to set.
   */
  public function setIsMaintained(bool $is_maintained);

  /**
   * Set whether the project is compatible with the current Drupal installation.
   *
   * @param bool $compatible
   *   Whether the project is compatible or not.
   */
  public function setIsCompatible(bool $compatible);

  /**
   * Warnings related to installing a given module.
   *
   * @param string[] $warnings
   *   Warnings about the module to present the the user.
   */
  public function setWarnings(array $warnings);

}
+16 −0
Original line number Diff line number Diff line
@@ -151,4 +151,20 @@ class ProjectBrowserPluginTest extends WebDriverTestBase {
    $this->assertEquals('Not compatible', $disabled_button->getText());
  }

  /**
   * Tests the detail page.
   */
  public function testDetailPageRandomDataPlugin(): void {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    $this->drupalGet('admin/modules/browse');
    $assert_session->waitForElementVisible('css', '#project-browser .project');
    $this->assertTrue($assert_session->waitForText('Results'));

    $first_project_selector = $page->find('css', '.project .project__title');
    $first_project_selector->click();
    $this->assertTrue($assert_session->waitForText('sites report using this module'));
  }

}