The FocalPoint class was not written with dependency injection in mind and this means that we cant write unit tests very well and that makes me sad. Fixing this would make me happy...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bleen’s picture

bleen’s picture

Status: Active » Needs work
bleen’s picture

Status: Needs work » Needs review
bleen’s picture

Chasing HEAD

bleen’s picture

This patch is getting closer. The biggest change is that there is no more getFocalPoints() method. It doesnt actually make sense to have this in the FocalPoint class... perhaps I should reintroduce it in the .module itself?

bleen’s picture

Chasing HEAD

bleen’s picture

phenaproxima’s picture

Status: Needs review » Needs work

At @balsama's request, I gave this a look. I'm unfamiliar with Focal Point so I didn't try to find deep issues or bugs, but I recommend the following changes:

  • +++ b/src/FocalPoint.php
    @@ -21,11 +21,11 @@ class FocalPoint {
    -  private $fid;
    +  private $file;
    

    Drupal doesn't use private; protected is greatly preferred.

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    -   * @param int $fid
    +   * @param File $file
    

    @param File should be @param \Drupal\file\FileInterface.

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    +  public function __construct(File $file) {
    

    Likewise, we prefer to type hint interfaces, not concrete classes, so that we can use mock objects in unit testing. So this should be FileInterface, not File.

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    -   * Implements \Drupal\focal_point\FocalPoint::getFocalPoint().
    +   * Implements \Drupal\focal_point\FocalPoint::setFocalPoint().
    

    This comment is strange -- is setFocalPoint() an abstract method or something? This line should probably just be iced.

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    +   * @param string $focal_point
    +   *
    +   * @param bool $save
    +   *
    +   * @throws \InvalidArgumentException
    

    These need more info :)

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    +   * Implements \Drupal\focal_point\FocalPoint::getFocalPoint().
    

    This line is kinda meaningless, since it's on a method called...FocalPoint::getFocalPoint() :) Should be removed.

  • +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    -   * @return array
    +   * @return string
    

    Needs more info about the meaning of the return value.

  • +++ b/src/FocalPoint.php
    @@ -109,37 +111,23 @@ class FocalPoint {
    +    if (!empty($this->focalPoint)) {
           \Drupal::database()->merge('focal_point')
    -        ->key(array('fid' => $this->fid))
    -        ->fields(array('focal_point' => $focal_point))
    +        ->key(array('fid' => $this->file->id()))
    +        ->fields(array('focal_point' => $this->focalPoint))
    

    Ideally, \Drupal::database() should not be used in a class. The database should be injected in the constructor -- \Drupal\Core\Database\Connection.

  • +++ b/src/FocalPoint.php
    @@ -208,4 +198,17 @@ class FocalPoint {
    +   * @param int $fid
    +   *
    +   * @return int|FALSE
    

    These need more info.

  • +++ b/src/FocalPoint.php
    @@ -208,4 +198,17 @@ class FocalPoint {
    +  protected function getFocalPointFromDatabase($fid) {
    +    return db_query('SELECT focal_point FROM {focal_point} WHERE fid = :fid', array(':fid' => $fid))->fetchField();
    

    Should not use db_query(). The database should be injected in the constructor so you can do $this->database->select().

  • +++ b/tests/src/Unit/FocalPointTest.php
    @@ -17,6 +18,55 @@ use Drupal\focal_point\FocalPoint;
    +    $file = $this->getMockBuilder('Drupal\file\Entity\File')
    +      ->disableOriginalConstructor()
    +      ->getMock();
    

    Generally, we prefer to mock interfaces over concrete classes. Also, if you mock FileInterface instead of File, you will not need to call disableOriginalConstructor().

  • balsama’s picture

    Thanks, @phenaproxima. @bleen18, we'll try to roll a new patch based on this unless you have objections or want to do it yourself. @chuchunaku can you take that on?

    ChuChuNaKu’s picture

    @balsama Yes, I'll work on that now.

    bleen’s picture

    yes, please .. patches welcome

    ChuChuNaKu’s picture

    @bleen18 I'm working on putting an updated patch together based on @phenaproxima's comments. A few of the comments stated that more info is needed. I was wondering if you could help me complete the missing information @phenaproxima mentioned above?

    +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    + * @param string $focal_point
    + *
    + * @param bool $save
    + *
    + * @throws \InvalidArgumentException
    These need more info :)

    +++ b/src/FocalPoint.php
    @@ -38,51 +38,51 @@ class FocalPoint {
    - * @return array
    + * @return string
    Needs more info about the meaning of the return value.

    +++ b/src/FocalPoint.php
    @@ -208,4 +198,17 @@ class FocalPoint {
    + * @param int $fid
    + *
    + * @return int|FALSE

    These need more info.

    Also, I attempted to apply the dependency_injection-2488292-6.patch but part of it failed. The errors I got when trying to apply the patch was

    patching file focal_point.module
    Hunk #2 FAILED at 27.
    1 out of 2 hunks FAILED -- saving rejects to file focal_point.module.rej
    patching file src/FocalPoint.php
    Hunk #4 FAILED at 111.
    1 out of 8 hunks FAILED -- saving rejects to file src/FocalPoint.php.rej
    patching file src/Plugin/Field/FieldWidget/FocalPointImageWidget.php
    patching file tests/src/TestFocalPoint.php
    patching file tests/src/Unit/FocalPointTest.php

    bleen’s picture

    This is a straight re-roll of the patch from #6 with a few extra docblock comments based on #12...

    I dont have a working local environment right now because my laptop is wonky and i have not had time to wipe it so this reroll has not been tested. (Incidentally, if anyone has an answer to this http://stackoverflow.com/questions/32878420/apache-wont-start-on-osx-htt... that'd be swell)

    ericduran’s picture

    +++ b/src/FocalPoint.php
    @@ -148,14 +140,16 @@ class FocalPoint {
         $this->flush($fid);
    

    This call isn't needed anymore.

    ericduran’s picture

    +++ b/tests/src/Unit/FocalPointTest.php
    @@ -17,6 +18,55 @@ use Drupal\focal_point\FocalPoint;
    +    $focal_point = new TestFocalPoint($file);
    

    Is it me or is the TestFocalPoint class missing?

    It's not in the git repo

    bleen’s picture

    FileSize
    11.74 KB

    I removed the line from #14

    The TestFocalPoint class is in the repo: http://cgit.drupalcode.org/focal_point/tree/tests/src/Unit?h=8.x-1.x

    no its not ... adding back to the patch

    bleen’s picture

    FileSize
    789 bytes

    Here is a patch with the TestFocalPoint class too

    bleen’s picture

    FileSize
    12.51 KB

    ... and finally, an actual patch with everything...

    bleen’s picture

    Status: Needs work » Closed (outdated)

    #2626950: Depend on Crop API for storage largely made this issue mute ... closing