summaryrefslogtreecommitdiff
path: root/core/tests/Item_Model_Test.php
blob: 40915360260824b7b8400b0cd77f05cfc2a64f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class Item_Model_Test extends Unit_Test_Case {
  public function saving_sets_created_and_updated_dates_test() {
    $item = self::create_random_item();
    $this->assert_true(!empty($item->created));
    $this->assert_true(!empty($item->updated));
  }

  private function create_random_item() {
    $item = ORM::factory("item");
    /* Set all required fields (values are irrelevant) */
    $item->name = rand();
    $item->type = "photo";
    return $item->add_to_parent(ORM::factory("item", 1));
  }

  public function updating_doesnt_change_created_date_test() {
    $item = self::create_random_item();

    // Force the creation date to something well known
    $db = Database::instance();
    $db->update("items", array("created" => 0, "updated" => 0), array("id" => $item->id));
    $item->reload();
    $item->title = "foo";  // force a change
    $item->save();

    $this->assert_true(empty($item->created));
    $this->assert_true(!empty($item->updated));
  }

  public function updating_view_count_only_doesnt_change_updated_date_test() {
    $item = self::create_random_item();
    $item->reload();
    $this->assert_same(0, $item->view_count);

    // Force the updated date to something well known
    $db = Database::instance();
    $db->update("items", array("updated" => 0), array("id" => $item->id));
    $item->reload();
    $item->view_count++;
    $item->save();

    $this->assert_same(1, $item->view_count);
    $this->assert_true(empty($item->updated));
  }

  public function move_photo_test() {
    // Create a test photo
    $item = self::create_random_item();

    file_put_contents($item->thumb_path(), "thumb");
    file_put_contents($item->resize_path(), "resize");
    file_put_contents($item->file_path(), "file");

    $original_name = $item->name;
    $new_name = rand();

    // Now rename it
    $item->rename($new_name)->save();

    // Expected: the name changed, the name is now baked into all paths, and all files were moved.
    $this->assert_equal($new_name, $item->name);
    $this->assert_equal($new_name, basename($item->file_path()));
    $this->assert_equal($new_name, basename($item->thumb_path()));
    $this->assert_equal($new_name, basename($item->resize_path()));
    $this->assert_equal("thumb", file_get_contents($item->thumb_path()));
    $this->assert_equal("resize", file_get_contents($item->resize_path()));
    $this->assert_equal("file", file_get_contents($item->file_path()));
  }

  public function move_album_test() {
    // Create an album with a photo in it
    $root = ORM::factory("item", 1);
    $album = album::create($root, rand(), rand(), rand());
    $photo = ORM::factory("item");
    $photo->name = rand();
    $photo->type = "photo";
    $photo->add_to_parent($album);

    file_put_contents($photo->thumb_path(), "thumb");
    file_put_contents($photo->resize_path(), "resize");
    file_put_contents($photo->file_path(), "file");

    $original_album_name = $album->name;
    $original_photo_name = $photo->name;
    $new_album_name = rand();

    // Now rename the album
    $album->rename($new_album_name)->save();
    $photo->reload();

    // Expected:
    // * the album name changed.
    // * the album dirs are all moved
    // * the photo's paths are all inside the albums paths
    // * the photo files are all still intact and accessible
    $this->assert_equal($new_album_name, $album->name);
    $this->assert_equal($new_album_name, basename($album->file_path()));
    $this->assert_equal($new_album_name, basename(dirname($album->thumb_path())));
    $this->assert_equal($new_album_name, basename(dirname($album->resize_path())));

    $this->assert_same(0, strpos($photo->file_path(), $album->file_path()));
    $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path())));
    $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path())));

    $this->assert_equal("thumb", file_get_contents($photo->thumb_path()));
    $this->assert_equal("resize", file_get_contents($photo->resize_path()));
    $this->assert_equal("file", file_get_contents($photo->file_path()));
  }
}