diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
index 1eaf4cc46..e2d2f90cf 100644
--- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
+++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
@@ -183,6 +183,7 @@
+
diff --git a/NzbDrone.Core.Test/TvTests/EpisodeProviderTests/HandleEpisodeFileDeletedFixture.cs b/NzbDrone.Core.Test/TvTests/EpisodeProviderTests/HandleEpisodeFileDeletedFixture.cs
new file mode 100644
index 000000000..d6c4e85fd
--- /dev/null
+++ b/NzbDrone.Core.Test/TvTests/EpisodeProviderTests/HandleEpisodeFileDeletedFixture.cs
@@ -0,0 +1,110 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using FizzWare.NBuilder;
+using Moq;
+using NUnit.Framework;
+using NzbDrone.Core.Configuration;
+using NzbDrone.Core.MediaFiles;
+using NzbDrone.Core.MediaFiles.Events;
+using NzbDrone.Core.Test.Framework;
+using NzbDrone.Core.Tv;
+
+namespace NzbDrone.Core.Test.TvTests.EpisodeProviderTests
+{
+ [TestFixture]
+ public class HandleEpisodeFileDeletedFixture : CoreTest
+ {
+ private EpisodeFile _episodeFile;
+ private List _episodes;
+
+ [SetUp]
+ public void Setup()
+ {
+ _episodeFile = Builder
+ .CreateNew()
+ .Build();
+ }
+
+ private void GivenSingleEpisodeFile()
+ {
+ _episodes = Builder
+ .CreateListOfSize(1)
+ .All()
+ .With(e => e.Monitored = true)
+ .Build()
+ .ToList();
+
+ Mocker.GetMock()
+ .Setup(s => s.GetEpisodeByFileId(_episodeFile.Id))
+ .Returns(_episodes);
+ }
+
+ private void GivenMultiEpisodeFile()
+ {
+ _episodes = Builder
+ .CreateListOfSize(2)
+ .All()
+ .With(e => e.Monitored = true)
+ .Build()
+ .ToList();
+
+ Mocker.GetMock()
+ .Setup(s => s.GetEpisodeByFileId(_episodeFile.Id))
+ .Returns(_episodes);
+ }
+
+ [Test]
+ public void should_set_EpisodeFileId_to_zero()
+ {
+ GivenSingleEpisodeFile();
+
+ Subject.Handle(new EpisodeFileDeletedEvent(_episodeFile));
+
+ Mocker.GetMock()
+ .Verify(v => v.Update(It.Is(e => e.EpisodeFileId == 0)), Times.Once());
+ }
+
+ [Test]
+ public void should_update_each_episode_for_file()
+ {
+ GivenMultiEpisodeFile();
+
+ Subject.Handle(new EpisodeFileDeletedEvent(_episodeFile));
+
+ Mocker.GetMock()
+ .Verify(v => v.Update(It.Is(e => e.EpisodeFileId == 0)), Times.Exactly(2));
+ }
+
+ [Test]
+ public void should_set_monitored_to_false_if_autoUnmonitor_is_true()
+ {
+ GivenSingleEpisodeFile();
+
+ Mocker.GetMock()
+ .SetupGet(s => s.AutoUnmonitorPreviouslyDownloadedEpisodes)
+ .Returns(true);
+
+ Subject.Handle(new EpisodeFileDeletedEvent(_episodeFile));
+
+ Mocker.GetMock()
+ .Verify(v => v.Update(It.Is(e => e.Monitored == false)), Times.Once());
+ }
+
+ [Test]
+ public void should_leave_monitored_to_true_if_autoUnmonitor_is_false()
+ {
+ GivenSingleEpisodeFile();
+
+ Mocker.GetMock()
+ .SetupGet(s => s.AutoUnmonitorPreviouslyDownloadedEpisodes)
+ .Returns(false);
+
+ Subject.Handle(new EpisodeFileDeletedEvent(_episodeFile));
+
+ Mocker.GetMock()
+ .Verify(v => v.Update(It.Is(e => e.Monitored == true)), Times.Once());
+ }
+ }
+}
diff --git a/NzbDrone.Core/Tv/EpisodeService.cs b/NzbDrone.Core/Tv/EpisodeService.cs
index ef996e09a..2ea42bc50 100644
--- a/NzbDrone.Core/Tv/EpisodeService.cs
+++ b/NzbDrone.Core/Tv/EpisodeService.cs
@@ -182,7 +182,12 @@ namespace NzbDrone.Core.Tv
{
_logger.Trace("Detaching episode {0} from file.", episode.Id);
episode.EpisodeFileId = 0;
- episode.Monitored = _configService.AutoUnmonitorPreviouslyDownloadedEpisodes;
+
+ if (_configService.AutoUnmonitorPreviouslyDownloadedEpisodes)
+ {
+ episode.Monitored = false;
+ }
+
UpdateEpisode(episode);
}
}